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
driver.implicitly_wait()? If so, what are you setting?time.sleep; these are not the same thing. Implicit waits are when you tell the driver "always wait x seconds to find elements". Whereas usingtime.sleepis telling the script to wait.visibility_of_element_locatedforpresence_of_element_located?