Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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 at 7:41
    
Similar to Anuragh27's question- Are you sure the "btnContr" class name is unique? –  Yash Apr 30 at 15:01
1  
Cannot it be done simpler, without JavaScript? E.g., driver.findElement(By.css("input[value='Cancel']")).click() –  dzieciou Jun 29 at 16:08

2 Answers 2

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(ExcelLibrary.java:35) at com.lmm.Executor.testExecute(Executor.java:104) at OnFirefoxBrowser.LISTING_CANCEL.LISTING_CANCEL(LISTING_CANCEL.java:18) 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
1  
This looks like a completely different problem, as NPE is thrown by ExcelLibrary,not WebDriver nor your code. –  dzieciou Jun 29 at 16:12
WebDriver driver; //instantiated somewhere else, obviously
WebElement element = driver.findElement(By.xpath("//div[@class='btnContr']/input[@value='Cancel']");
element.click();

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

share|improve this answer

Your Answer

 
discard

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.