0

I have a very strange behaviour happening while automating a test suite were I access a website, click on a search bar and then look for certain information.

All my 200+ tests begin the same way with the exact same code and the first 177 pass perfectly but from test 178 and onwards it fails in the line of code where i find the search bar and then click on it.

To me this makes no sense but again, I´m quite new to automation and IT in general so I hope you people can give me a hand here.

The line that fails is the following one:

WebDriverWait(self.driver, 40).until(EC.presence_of_element_located((coupons_search_bar))).click()

Again, this works for 177 tests straight and then it fails in the following ones.

Thanks!

I changed the way I get to the coupons search bar in many ways but they work for the 177 first tests but fail for the following ones.

I cleaned the local storage and cache and nothing changed.

Martin M
  • 1
  • 2
  • In these scenarios, it's invaluable to watch the execution of the test to actually see what's happening. that's where services like BrowserStack or SauceLabs are helpful because you get a recording. Alternatively, set up your test execution to capture a screenshot of the browser at the point that it failed. – Breaks Software Jun 28 '23 at 12:17

2 Answers2

0

Ideally to locate and click on the clickable element instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Line of code:

    WebDriverWait(self.driver, 40).until(EC.element_to_be_clickable((coupons_search_bar))).click()
    
  • 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
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

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

Without additional debugging details, there are different reasons why your script may start failing after X number of times. Here are some common ones:

  • Too many requests on the same website raised a pop-up. In which case you would see an ElementClickInterceptedException (other element would receive the click) until you close the pop-up first.

  • Too many requests on the same website triggered anti-bot measures. (Because some sites don't want to be automated.) Maybe do some rate-limiting on your requests.

  • Other elements appeared, changing timing. Swap EC.presence_of_element_located with element_to_be_clickable.

  • The website is doing A/B testing, which means another version of the site appeared. In that scenario, add in an if statement to change your script depending on what's visible.

Please edit your question with more relevant info, such as adding exception messages, the selector used, and any other details that may help with debugging if none of the above ideas helped.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48