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?

  • 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
  • 1
    Cannot it be done simpler, without JavaScript? E.g., driver.findElement(By.css("input[value='Cancel']")).click() – dzieciou Jun 29 '14 at 16:08
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()");
  • 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 '14 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);
  • Is this meant to answer the question? – Swagin9 May 2 '17 at 15:31
  • Is this an answer to the question or is it just a thank you comment? – IAmMilinPatel May 30 '17 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 May 30 '17 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 Jun 1 '17 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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