user2432405
user2432405

Reputation: 1414

Selenium WebDriver(Java) : JavascriptExecutor fails to click element, while WebElement.click() works well

As the following code shows, clicking the element by the WebElement.click() method works, while JavascriptExecutor.executeScript fails ( works in most situaitons ).

WebElement e = driver.findElement(By.xpath("......."));//some kind of checkbox
e.click(); //works fine.
((JavascriptExecutor) aw.driver).executeScript("arguments[0].click();",e); 
//executes with no exception, but the element is not selected.

The page's source code is too complex to paste here, sorry.
Any clue or tip for debugging this problem?

The element's html code, It's a extjs-style checkbox:
<td class="x-grid3-hd x-grid3-cell x-grid3-td-checker x-grid3-cell-first " style="width: 20px;"> <div class="x-grid3-hd-inner x-grid3-hd-checker" id="ext-gen108" unselectable="on" _nodup="30805"> <div class="x-grid3-hd-checker" _nodup="30805" /> </div></td>

I have tried click each of td, td/div , td/div/div elements , getting same result: WebElement.click() works, JavascriptExecutor.executeScript fails(No exception, but the checkbox is not checked).

Upvotes: 0

Views: 1804

Answers (2)

nitin chawda
nitin chawda

Reputation: 1388

Instead use document.evaluate() for JavaScript executor method.something like this: executeScript("document.evaluate( \"" ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue.click()");

Upvotes: 0

IanChiu
IanChiu

Reputation: 11

I would not assume the JavaScriptExecutor failed to execute your Script first, but see if the Script is working as expected.

Try to set break point at "e.click();" and then start debugging, after that try to run your Script (which is "arguments[0].click();" in your case) in browser console and see what happened.

If you have confirmed the script is good, then try to put break point at your executeScript and make sure that line of code have been run successfully.

Upvotes: 1

Related Questions