-1

My code which takes me to the button and highlights it (I'm running java scripts within Eclipse)

driver.findElement(By.xpath("//button[contains(text(),'Save')]")).click();

My HTML code

<button data-v-7405e096="" type="button" class="pull-right btn btn-primary">Save</button>

The error

Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //button[contains(text(),'Save'‌​)] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//button[contains(text(),'Save'‌​)]' is not a valid XPath expression

  • <button class="pull-right btn-primary" data -v-7814d041="" type="button">Save</button> this is the html code – Shameera Rassool Jan 18 '18 at 13:16
  • no error when running – Shameera Rassool Jan 18 '18 at 13:18
  • no just highlights the save button doesnt perform the click – Shameera Rassool Jan 18 '18 at 13:19
  • how would i use that? – Shameera Rassool Jan 18 '18 at 13:23
  • im running java scripts within eclipse – Shameera Rassool Jan 18 '18 at 13:23
2

First Check the button is able to be clickable at the moment of your try. Sometimes the page load is very long it's not clickable at the moment.

Use the below code and check

WebDriverWait wait = new WebDriverWait (driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Save')]"))).click();
  • still doesnt work, i dont get the save part to work, with the above code, when i use mine, atleast i get to the button it highlights it. my code: driver.findElement(By.xpath("//*[contains(text(), 'Save')]")).click(); – Shameera Rassool Jan 19 '18 at 7:50
  • @ShameeraRassool, just try to replace the XPath as you say "//*[contains(text(), 'Save')]" on the above code and let me know any issues. – MohanRaj Jan 19 '18 at 8:58
  • @ShameeraRassool - Can you please accept the answer that resolved your question? Or, if you resolved it in another way, answer the question yourself and accept it. Thanks. – trashpanda Jan 19 '18 at 10:41
1
  1. first check the xpath is correct?
  2. second check if a button is enabled or disabled?
  3. then click the button.

Sometimes, the button is not clickable and our script fails.

0

Try JavaScript executors as below,

WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Save')]"));
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element); 

If this doesn’t work give a wait until the element is clickable then try above code again after wait

0

Use assertion that the button exists, loaded and clickable. And then try to find the button by it's text.

Something like that:

driver.findElement(By.text("Save")).click();

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.