-1

Whenever I am using explicit wait, at times data is not getting saved in the variable:

validation_message = WebDriverWait(context.driver, 30).until(
       EC.element_to_be_clickable((By.XPATH, '//body/div[1]/div[1]/span[1]'))).text


assert (validation_message == 'Password must have at least 7 characters, 1 number, 1 uppercase letter, 1 lowercase letter and one special character.')
    print(validation_message)
1
  • 3
    Is there a reason you're waiting for it to be clickable instead of visible? If it's just text, it's not an interactive element and it could have elements that overlay it, preventing it from being clickable, but allowing it to be visible.
    – Roger Cook
    Dec 5, 2022 at 16:59

2 Answers 2

0

There could be many reasons why the driver is failing to find the validation_message element. You might need to decompose the problem a bit more and clarify the question to help provide some more context. What are you expecting to happen? What are we actually waiting for here in the test? e.g. is it a long running network request or an element render?

Implicit time based waits can be helpful for debugging failing tests. However the use of waits with arbitrary time values is generally regarded as an anti-pattern in UI test automation. The Selenium documentation explains that a combination of both implicit and explicit waits is also not recommended as it may result in inconsistent wait behaviour.

2
  • Actually trying to read text inside a validation message, Once user puts some invalid data then a validation message is thrown from backend and gets displayed on frontend in form of a toast Dec 5, 2022 at 16:54
  • @HassanBari I'm assuming that there is a network request of some kind involved to a backend service as part of this validation. It might be worth investigating this request in more detail to determine if it's delaying or preventing the display of the UI element. E.g. Is the request failing? Does WebDriver timeout before a response is returned from the backend service? Dec 7, 2022 at 10:37
0

If you are finding that the explicit wait is unpredictable, here are a few steps you can take to debug the issue:

  1. Verify that the element is present and clickable on the page before the wait: You can use driver.find_element_by_xpath to verify that the element is present on the page. If the element is not present, the script will throw an exception.

  2. Increase the wait time: If the element is present, but the wait time is too short, increasing the wait time may help.

  3. Use an Expected Condition that is more specific to your needs: For example, instead of using EC.element_to_be_clickable, you could use EC.visibility_of_element_located to wait for the element to be visible, or EC.text_to_be_present_in_element to wait for the text to be present in the element.

  4. Add a catch for the exception: If the wait throws an exception, you can add a catch block to handle it.

  5. Debug with logs and prints: Add print statements and logs to check the values of validation_message and other variables at various stages in the code. This will help you understand why the data is not being saved in the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.