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?
5 Answers 5
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– user66332013年12月31日 11:52:50 +00:00Commented Dec 31, 2013 at 11:52
-
1This looks like a completely different problem, as NPE is thrown by ExcelLibrary,not WebDriver nor your code.dzieciou– dzieciou2014年06月29日 16:12:23 +00:00Commented Jun 29, 2014 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?
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);
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);
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– Swagin92017年05月02日 15:31:05 +00:00Commented May 2, 2017 at 15:31
-
Is this an answer to the question or is it just a thank you comment?IAmMilinPatel– IAmMilinPatel2017年05月30日 02:59:05 +00:00Commented 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.2017年05月30日 04:59:01 +00:00Commented 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.2017年06月01日 15:55:05 +00:00Commented Jun 1, 2017 at 15:55
Explore related questions
See similar questions with these tags.
driver.findElement(By.css("input[value='Cancel']")).click()
document.getElement()
. It isdocument.getElementById()
. For finding element by xpath using javascriptexecutor you have to useWebElement element=(WebElement)jse.executeScript("document.evaluate('xpath here')")
. Check the syntax of evaluate. It also takes some more arguments.