0

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?

4
  • Is this the only Input tag inside that Div or does it contain other Input elements too? Commented Jan 1, 2014 at 7:41
  • Similar to Anuragh27's question- Are you sure the "btnContr" class name is unique?
    – Yash
    Commented Apr 30, 2014 at 15:01
  • 1
    Cannot it be done simpler, without JavaScript? E.g., driver.findElement(By.css("input[value='Cancel']")).click()
    – dzieciou
    Commented Jun 29, 2014 at 16:08
  • there is no method like document.getElement(). It is document.getElementById(). For finding element by xpath using javascriptexecutor you have to use WebElement element=(WebElement)jse.executeScript("document.evaluate('xpath here')"). Check the syntax of evaluate. It also takes some more arguments.
    – a Learner
    Commented Dec 29, 2021 at 13:08

5 Answers 5

1

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()");
2
  • 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
    Commented Dec 31, 2013 at 11:52
  • 1
    This looks like a completely different problem, as NPE is thrown by ExcelLibrary,not WebDriver nor your code.
    – dzieciou
    Commented Jun 29, 2014 at 16:12
0
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?

0

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);
0

I was looking to click and send text by using xpath in JSE. Here is how I've done it:

WebElement element = driver.findElement(By.xpath("//input[@title='Search']"));
WebElement element1 = driver.findElement(By.xpath("//a[text()='Gmail']"));
String hi = "Hi";

JavascriptExecutor jse = (JavascriptExecutor) driver;

jse.executeScript("arguments[0].click();", element1);  //for clicking

// for Entering text
jse.executeScript("arguments[0].value='"+hi+"';", element); 
0

This works for me:

WebElement elmnt =driver.findElement(By.xpath("//*[@id='signOutLink']"));   
JavascriptExecutor js = (JavascriptExecutor) driver;  
js.executeScript("arguments[0].click();", elmnt);
4
  • Is this meant to answer the question?
    – Swagin9
    Commented May 2, 2017 at 15:31
  • Is this an answer to the question or is it just a thank you comment? Commented May 30, 2017 at 2:59
  • This answer looks like a just copy paste of the above answer which was earlier answered by @Shubham Jain. If you have resolved your issue, just accept the answer rather than the new answer.
    – Bharat Mane
    Commented May 30, 2017 at 4:59
  • The important part (the xpath) is different in this than in the other answer. Even if it was inspired by that answer as a jumping off point, it looks like the key part is different. I would contend (all else being equal) that this is an honest attempt at an answer.
    – corsiKa
    Commented Jun 1, 2017 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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