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

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

5 Answers 5

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();
3
  • 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(); Commented Jan 19, 2018 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.
    – Raj
    Commented Jan 19, 2018 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.
    – dvniel
    Commented Jan 19, 2018 at 10:41
1

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

1
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

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

I came across this issue building a test suite for our own application which uses the Angular JS Framework.

Using the following helper functions:

private IWebElement FindElementWithWait(By id, int timeout)
   {
       bool found = false;
       int loop = 0;
       IWebElement element = null;
       var i = timeout * 10;
       while (!found && loop < i)
       {
           try
           {
               element = Driver.FindElement(id);
               found = true;
           }
           catch (Exception e)
           {
               found = false;
           }
           finally
           {
               loop++;

               Thread.Sleep(100);
           }
       }

       return element;
   }

private void JavascriptClick(IWebElement element)
   {
       Js.ExecuteScript("arguments[0].click();", element);
   }

It's possible to overcome the issue where elements using ng-click cause an "element not interactable" exception when calling the standard IWebElement .Click() method.

e.g. JavascriptClick(FindElementWithWait(By.XPath("//button[text()='My_ng-click_Button']"), 2));

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.