admin管理员组

文章数量:1314292

I have a button who's code is as follows:

<input type="button" onclick="someFunction();" value="doSomething">

I am trying to click on this button in Selenium2. I manage to get the element, but the click() function does not work in Chrome.

I tried to do it on JavaScript directly, but it still doesn't work in Chrome.

It works if you click directly on the button, and it also works in Firefox.

Anyone has any idea on how to fix this?

Edit: I had forgotten the brackets in my example. They're present in the "real" code.

Edit2: The Selenium 2 code used to simulate the click is:

driver.findElement(By.tagName("input")).click();

Before somebody asks, there is no other tagName "input" in the page.

I have a button who's code is as follows:

<input type="button" onclick="someFunction();" value="doSomething">

I am trying to click on this button in Selenium2. I manage to get the element, but the click() function does not work in Chrome.

I tried to do it on JavaScript directly, but it still doesn't work in Chrome.

It works if you click directly on the button, and it also works in Firefox.

Anyone has any idea on how to fix this?

Edit: I had forgotten the brackets in my example. They're present in the "real" code.

Edit2: The Selenium 2 code used to simulate the click is:

driver.findElement(By.tagName("input")).click();

Before somebody asks, there is no other tagName "input" in the page.

Share Improve this question edited Aug 28, 2012 at 13:48 Stilltorik asked Aug 28, 2012 at 13:34 StilltorikStilltorik 1,6924 gold badges19 silver badges31 bronze badges 2
  • 2 @dystroy I can't imagine removing the semicolon would help. It still wouldn't be called, would it? – Waleed Khan Commented Aug 28, 2012 at 13:37
  • Try modifying your code to this: <input type="button" onclick="someFunction();" value="doSomething" /> – Shmiddty Commented Aug 28, 2012 at 13:43
Add a ment  | 

4 Answers 4

Reset to default 6

Generally speaking, using inline attributes to define event handlers is a bad idea. Instead you should opt for binding event handlers using JavaScript so that your content (HTML) and functionality (JavaScript) are separated.

However, the problem seems to be that you're not actually calling the function. Change it to:

<input type="button" onclick="someFunction();" value="doSomething">

i think you have to use function name with function brackets()

like this input type="button" onclick="someFunction();" value="doSomething"

i hope this will help you

Try like this, or even better put javascript in separate file:

<input type="button" value="doSomething" id="someFuncBtn">

<script type="text/javascript">
    $(document).ready(function () {
        $('#someFuncBtn').on('click', function(){
            someFunction();
        });
    });
</script>

I realised that it was a problem with the click, that works a bit oddly sometimes. It works better this way:

driver.findElements(By.tagName("input")).get(0).click();

Even though it still fails "sometimes". I still have no idea why though.

本文标签: seleniumJavascript can39t click() on an ltinput typequotbuttonquotgt in ChromeStack Overflow