1

We have an automated UI test running with BrowserStack and it does not run consistently occasionally timing out while waiting for elements to be added to the DOM, but when running locally with Chromedriver the test succeeds 100% of the time.

Here is an example of the selenium wait that is failing:

return WebDriverWait(self.__driver, 10).until(
    ec.presence_of_element_located((By.XPATH, locator))
)

Has anyone else seen any similar inconsistency with BrowserStack and know what might be causing it, and is there a better way to wait for the element to become available.

The exact exption we are getting is a TimeoutException from ec.presence_of_element_located((By.XPATH, locator)).

Evan Snapp
  • 523
  • 5
  • 21
  • Where is your Web application hosted? Is it internal servers or cloud? ... Might seem obvious but Have you tried increasing the timeout? – RichEdwards Aug 03 '20 at 17:20
  • The app is hosted on a cloud service, and BrowserStack is able to reach the site without issue. Increasing the timeout to 40 seconds causes it to fail less, but it is still not reliable. The test always succeeds locally with ChromeDriver. – Evan Snapp Aug 03 '20 at 17:45
  • Are you running things in parallel in browserstack? - running locally is typically single thread but the moment you do things in parallel there is potential for data overlap. ( i know these are open questions - but there's no easy answer, it's to tease out information in regards to what is different between a local i7 machine vs a remote virtual box that needs to be as cheap and efficient as possible) :-) – RichEdwards Aug 03 '20 at 17:51
  • No, there is not anything running in parallel, that is one thing we were considering adding in the future, but currently, everything is single-threaded. Thank you though, that is a good thought! – Evan Snapp Aug 03 '20 at 17:52

2 Answers2

2

presence_of_element_located()

presence_of_element_located() is the expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible. Hence the wait for the very first matched WebElement to be present.


Generic usecases

Generally, in your tests simply waiting for presence of element may not suffice to your requirement and you may need to wait for the element either to be visible or interactable.

To wait until the element is visible, you need to induce WebDriverWait for the visibility_of_element_located() and your effective code block will be:

return WebDriverWait(self.__driver, 10).until(
    ec.visibility_of_element_located((By.XPATH, locator))
)

To wait until the element is interactable, you need to induce WebDriverWait for the element_to_be_clickable() and your effective code block will be:

return WebDriverWait(self.__driver, 10).until(
    ec.element_to_be_clickable((By.XPATH, locator))
)

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec

This usecase

In this usecase, the line:

ec.presence_of_element_located((By.XPATH, locator))

was unable to identify the desired element within the desired time frame hence you faced TimeoutException.

However, from TimeoutException it will be tough to dig out the actual result of the failure.


Solution

As a solution to know about the exact cause of the failure, you need to remove the WebDriverWait and replace the line of code with either:

  • find_element_by_class_name(name)
  • find_element_by_css_selector(css_selector)
  • find_element_by_id(id)
  • find_element_by_link_text(link_text)
  • find_element_by_name(name)
  • find_element_by_partial_link_text(partial_link_text)
  • find_element_by_tag_name(tag_name)
  • find_element_by_xpath(xpath)

If required you can slow down the search inducing waits through time.sleep(secs) while debugging.


References

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

This issue has been resolved. For some reason switching the OS BrowserStack to OS X solved the error. Local tests with chrome-driver work for both operating systems, but our BrowserStack test only consistently works on Mac, with Windows failing about half the time. We are still working to understand exactly why this is, but currently, that was the fix.

Evan Snapp
  • 523
  • 5
  • 21