'How to solve an explicit wait which isn't waiting long enough selenium python
I'm trying to program a sequence of events which are dependent on the last in selenium. First click login which loads a new page, then click a scrollbox on that page, then click a button inside the scrollbox which won't be loaded until the scrollbox has been clicked.
I am trying to stop using time.sleep(x) as I've read this is bad practice and I'm trying to learn more about how selenium works.
The code I've got that doesn't work is
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as ec
login_box = WebDriverWait(driver, 5).until(
ec.element_to_be_clickable((By.XPATH, "//*[@id=\"loginAppRoot\"]/div[1]/div[1]/button/span")))
login_box.click()
print("login")
scroll_box = WebDriverWait(driver, 5).until(
ec.element_to_be_clickable((By.XPATH, "//*[@id=\"searchBarFilterDropdown\"]")))
scroll_box.click()
box_inside_scroll = WebDriverWait(driver, 5).until(
ec.element_to_be_clickable((By.XPATH, "//*[@id=\"global-header\"]/nav[1]/div[2]/div/div[1]/ul/li[44]/a")))
box_inside_scroll.click()
The only way that I can get this to work is to put a time.sleep(2)
before scroll_box.click()
. From my understanding the webdriver wait and expected condition should negate the need for me to use time.sleep. Could anyone help me to remove the pre-defined wait times?
Solution 1:[1]
If you need selenium to wait longer you just have to change the timeout. If all you need is 2 seconds more then try
scroll_box = WebDriverWait(driver, 7).until(
ec.element_to_be_clickable((By.XPATH, "//*[@id=\"searchBarFilterDropdown\"]")))
scroll_box.click()
when using WebDriverWait, in the parenthesis, your first input is your driver, second is the amount of time you want selenium to wait before it times out. Instead of 7 you can try 10, so it waits 10 seconds before attempting to click the scroll box.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | DDUffy |