Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this HTML

<div class="btnContr">    
  <input type="button" onclick="return cancel();" value="Cancel"/>

Below is the xpath identified, using this xpath, I am not able to click on the button.

Xpath=//div[@class='btnContr']/input[@onclick='return cancel();']

Code written:

JavascriptExecutor jse = (JavascriptExecutor)ff;
            jse.executeScript("document.getElement(By.xpath('//div[@class='btnContr']/input[@onclick='return cancel();']').click()");

What should my code be?

share|improve this question
Is this the only Input tag inside that Div or does it contain other Input elements too? – Anuragh27crony Jan 1 '14 at 7:41
Similar to Anuragh27's question- Are you sure the "btnContr" class name is unique? – Yash Apr 30 '14 at 15:01
Cannot it be done simpler, without JavaScript? E.g., driver.findElement(By.css("input[value='Cancel']")).click() – dzieciou Jun 29 '14 at 16:08

The problem is in your use of quotes - they don't match correctly. Since you're using three levels of quotes (around document.getElement; around the xpath; and for the attributes) you'll need to escape one pair.

jse.executeScript("document.getElement(By.xpath(\"//div[@class='btnContr']/input[@onclick='return cancel();']\").click()");
share|improve this answer
Tried your sollution. But still getting NullPointer Exception. It is as below java.lang.NullPointerException at com.lmm.ExcelLibrary.readExcelValue( at com.lmm.Executor.testExecute( at OnFirefoxBrowser.LISTING_CANCEL.LISTING_CANCEL( at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) – user6633 Dec 31 '13 at 11:52
This looks like a completely different problem, as NPE is thrown by ExcelLibrary,not WebDriver nor your code. – dzieciou Jun 29 '14 at 16:12
WebDriver driver; //instantiated somewhere else, obviously
WebElement element = driver.findElement(By.xpath("//div[@class='btnContr']/input[@value='Cancel']");;

Why are you bothering with JavascriptExecutor, when Selenium has the functionality to click an element selected by XPath built in?

share|improve this answer

I like to use below format to avoid quotes errors

WebElement element= driver.findElement(By.xpath("//div[@class='btnContr']/input[@value='Cancel']"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

Hope it will help you :)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.