JavascriptExecutor is not identifying the button to click in Webdriver



  • 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?



  • 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()");



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2