Reputation: 38
So I am populating a list of elements and then I am iterating through each element in the list. How do I implement a wait condition to tell the code to wait 15 seconds before failing to find the element? Specifically I want currentprice and title to try and wait until element is visible rather than just hard execute when it hits that line of code.
elements = driver.find_elements_by_css_selector('div.list-wrap div.item-container')
print(len(elements))
for x in range(len(elements)):
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR,'div.list-wrap div.item-container')))
elements = driver.find_elements_by_css_selector('div.list-wrap div.item-container')
currentprice = elements[x].find_element_by_css_selector('li.price-current').text
title = elements[x].find_element_by_css_selector('a.item-title').text
...
I tried the following but I am getting the error that WebDriverWait is not a method of elements.
elements[x].WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR,'li.price-current')))
Upvotes: 1
Views: 409
Reputation: 1876
You were close.
The WebDriverWait
expect a driver instance as first parameter. The WebElement
object has the driver object in its instance and also implement most of the WebDriver methods.
Having that in mind, you can create a new WebDriverWait
and pass your element instead of the driver.
Instead of:
elements[x].WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR,'li.price-current')))
Use:
WebDriverWait(elements[x], 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR,'li.price-current')))
Upvotes: 2