Home >Backend Development >Python Tutorial >Why is My Selenium WebDriverWait Failing to Find Elements in a Newly Opened Window?
WebDriverWait Not Functioning as Predicted
You're utilizing selenium to extract data from a webpage. The page contains a "custom_cols" button that launches a window for column selection. This new window occasionally takes several seconds (specifically, around 5 seconds) to load.
To handle this delay, you've employed WebDriverWait with a 20-second delay. Strangely, it occasionally fails to locate elements within the new window, despite the element being displayed on the screen. This issue occurs approximately once every ten attempts.
Interestingly, using WebDriverWait elsewhere in your code works as expected, ensuring elements are visible before clicking them.
Your query revolves around why elements within the new window are not recognized as visible despite employing WebDriverWait to detect their visibility.
Proposed Solution:
The problem stems from the fact that you're using WebDriverWait's presence_of_element_located() method instead of the element_to_be_clickable() method when attempting to click the element.
To address this, replace the following line:
myElem = WebDriverWait(self.browser, delay).until(EC.presence_of_element_located((By.XPATH , xpath)))
with:
myElem = WebDriverWait(self.browser, delay).until(EC.element_to_be_clickable((By.XPATH , xpath)))
Understanding the WebDriverWait Methods:
The following provides further clarification on the three mentioned methods:
The above is the detailed content of Why is My Selenium WebDriverWait Failing to Find Elements in a Newly Opened Window?. For more information, please follow other related articles on the PHP Chinese website!