0

I am new to Selenium and Python. I am able to navigate through a website, find an element and print it but it is running slow & sometimes fails when the server takes too long to respond because I am using time.sleep(3) throughout my code instead of Explicit Wait. I have never been able to get the Explicit Wait to work.

Python version: 3.10; Selenium Webdriver: Firefox; IDE: PyCharm 2021.3.2 (CE); OS: Fedora 35 VM

HTML code of an element I am trying to print:

<input id="b8-b36-Input_RemainAmtYr1"
class="form-control OSFillParent" data-
input="" disabled="" type="text"
style="margin-top: 5px;" value="$10.50">
event

My Python Selenium Locate code that works most of the time (but fails when the server takes too long to respond):

print('Remaining 2022 Deductible:',driver.find_element(By.CSS_SELECTOR, "input.form-control.OSFillParent[id$='Input_RemainAmtYr1']").get_attribute("value"))

My Explicit Wait code that produces errors:

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.form-control.OSFillParent[id$='Input_RemainAmtYr1']"))).get_attribute("value"))

This is the Traceback that is produced from my Explicit Wait code:

Traceback (most recent call last): File "/home/usr/Documents/Training/Python_Automation_Testing/Exercise Files/print_value.py", line 122, in print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.form-control.OSFillParent[id$='Input_RemainAmtYr1']"))).get_attribute("value")) File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/support/wait.py", line 89, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

Note : I am using 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

13
  • Have you set driver.implicitly_wait()? If so, what are you setting? Commented Mar 11, 2022 at 17:49
  • Also, you mention you using implicit waits but also mention using time.sleep; these are not the same thing. Implicit waits are when you tell the driver "always wait x seconds to find elements". Whereas using time.sleep is telling the script to wait. Commented Mar 11, 2022 at 17:53
  • 1
    Do you get the same error if you change out visibility_of_element_located for presence_of_element_located? Commented Mar 14, 2022 at 17:48
  • 1
    @Auditor to answer your question 'have I used presence', yes but only in situations where it made sense to do so. Like if I'm looking for an element that should be invisible. I didn't suggest you change your test to use presence, I only asked because it gives you something to troubleshoot as you begin to figure out why visibility wasn't working. It's usually a clue for me to look at the timing of things. Or is it possible there is a layer on top of the element in question that is preventing it from being considered 'visible'? Commented Mar 15, 2022 at 22:08
  • 1
    I would normally advise doing whatever actions need to be taken to make the element visible first. Commented Mar 16, 2022 at 14:04

1 Answer 1

1

Changing Tabs

From your comments, it sounds as if you are opening a new tab and the element you are looking for is on the page of that tab.

In which case you need the driver to switch to that tab before looking for the element:

driver.switch_to.window(driver.window_handles[-1])

*Change the index of window_handles to the tab you want to focus on

Also note that after you have finished on that tab (i.e you have closed it and gone back to the original tab), you will need to tell the driver to switch back again using the same line of code.

TimeoutException returned as boolean

Anyone visiting this page because they want the TimeoutException to return a boolean value instead of throwing an exception, you can try something like this:

#return boolean value instead of throwing exception
def check_elem_exists(parent, by, selector, wait=False, timeout=False):
    if not wait:
        try:
            parent.find_element(by, selector)
        except NoSuchElementException:
            return False
        else:
            return True
    #when allowing wait time - print note for what is happening
    print('%s - check_elem_exists(%is..)' %(selector,timeout))
    try:
        WebDriverWait(parent, timeout).until(EC.presence_of_element_located((by, selector)))
    except NoSuchElementException:
        return False
    except TimeoutException:
        return False
    else:
        return True

#search without wait time
elem1 = check_elem_exists(driver, By.XPATH, "//div[@class='elem1']")
#search with wait time (i.e waiting for page to load)
elem2 = check_elem_exists(driver, By.XPATH, "//div[@class='elem2']", True, 10)

Find useful XPATH cheatsheet:
https://devhints.io/xpath

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.