0
votes

I'm writing some tests for our web application. When I use this XPath inside the Firefox XPath-Checker plugin it just works fine:

//div[contains(@class, "my-class") and @data-custom-attr="id")]

But If use this in selenium code like so:

(new WebDriverWait(driver, MAX_TIMEOUT_FOR_ELEMENT_LOADING_IN_SECONDS)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(@class, \"my-class\") and @data-custom-attr=\"id\")]"));

a timeout exception is throwen.

Why does the xpath query works directly in the browser but not in the selenium test? Is there documentation on what I have to consider?

As side note: if I use the exact path /html/body/div/... etc. it does work. And a css selector pendant to my example above doesn't work either.

1
We would need to see a copy of the page you are testing against or run this against a public facing site. You could always pop that XPath into the Selenium IDE and see what it does. Is it in an IFrame? - Arran
Maybe the problem is not locating the element - but timing. What is value of MAX_TIMEOUT_FOR_ELEMENT_LOADING_IN_SECONDS ? Is it enough time for the element to become present? - Dingredient
10 seconds is more than enough. When I ran it on my local browser I see the element. - schlingel
It seems to me that the XPath-Parser in Selenium is bothered by the '-' in my attribute names. If I run the query which runs without any problems in the Xpath-Plugin in FF it works, If I do so in the Selenium IDE or my Java Selenium tests it fails. - schlingel

1 Answers

0
votes

It should work, what's the max wait time you given in code.