0

I tried this xpath to retrieve a button

By addGhostButton = By.xpath("//button[@data-test-id=\"order\"]");

Selenium did not find it, I had this error invalid selector: Unable to locate an element with the xpath expression

Maybe the syntax is wrong ? any help please?

2 Answers2

0

You haven't provided the complete error stack trace and the relevant HTML. However, the you constructed is pretty much valid and legit. Hence you shouldn't see invalid selector error but Unable to locate an element with the xpath expression is pretty much possible.

The desired element looks to be a dynamic element so to locate the element you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • Using cssSelector:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button[data-test-id='order']")));
    
  • Using xpath:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@data-test-id='order']")));
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
0

Try with this xpath //button[Contains(text(),"Précommande maintenant")]`

Justin Lambert
  • 839
  • 1
  • 7
  • 11