0
votes

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
By.xpath("//button[@data-test-id='order']") - Wilfred Clement
tried it, did not work - Ben Mustapha Sabrine
Are you sure the element is present ? Paste the dom here - Wilfred Clement
<button class="sc-htpNat dNtmvw" data-test-id="order" type="submit"><span class="button-loader sc-ifAKCX hBcjPR"></span><span class="sc-bwzfXH dbjywt">Précommande maintenant</span></button> - Ben Mustapha Sabrine
There seems to be no issue with the given xpath, Just add a wait and it should hopefully work - Wilfred Clement

2 Answers

0
votes

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']")));
    
0
votes

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