I am trying without success to click on a particular button using SELENIUM & JAVA but i am getting this error message:

Expected condition failed: waiting for element to be clickable: By.xpath: //*[@id="documentation"]/div/div[2]/div/button (tried for 10 second(s) with 500 milliseconds interval)

That button has this:

<button type="button" class="btn btn-sm btn-link add-row">Upload FILE</button>

The XPATH is:

//*[@id="documentation"]/div/div[2]/div/div/button

I did this:

WebDriverWait wait10735 = new WebDriverWait(driver,Duration.ofSeconds(10));
        JavascriptExecutor executor3735  = (JavascriptExecutor)driver;
        WebElement elementCat4735=wait10735.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"documentation\"]/div/div[2]/div/button")));

What am i doing wrong in here?

1

There are 1 answers

7
Jaky Ruby On BEST ANSWER

If you use the xpath:

//button[text()='Upload FILE']

Should work.

Why this happened?

The problem is that you were using the xpath:

xpath //*[@id=\"documentation\"]/div/div[2]/div/button

With that xpath what you are doing is:

  1. Locating an element with id="documentation"
  2. Then go to the first div child of that element
  3. Then go to second div child of the previous div
  4. Then go to the first div child of the previous div
  5. Then go to the button

As you can see your final element (The button) is depending on several other parent elements to be located. Once one of those parent elements changes, your xpath is not valid anymore.

Is much better to locate your xpath based on the properties of your element itself, not on the elements that surround it.

In this case I used the text of the element.

I don't know if it is possible or not, but if it is possible, a good idea is to talk with developers for using ids for all the elements you will use with your automation. This is a team work!