SIM
SIM

Reputation: 22440

Unable to use "explicit wait" in the right way

I've written a script using python with selenium to click on some links listed in the sidebar of google maps. When any of the items get clicked, the related information attached to each lead shows up in the right sided area. The script is doing fine. However, I've used hardcoded delay to do the job. How can I get rid of hardcoded delay by achieving the same with explicit wait. Thanks in advance.

Link to the site: website

The script I'm trying with:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

link = "replace_with_above_link"

driver = webdriver.Chrome()
driver.get(link)
wait = WebDriverWait(driver, 10)

for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "[id^='rlimg0_']"))):
    item.location
    time.sleep(3)  #wish to try with explicit wait but can't find any idea
    item.click()

driver.quit()

I tried with wait.until(EC.staleness_of(item)) instead of hardcoded delay but no luck.

Upvotes: 1

Views: 131

Answers (1)

Andersson
Andersson

Reputation: 52685

If you want to wait until new data displayed after each clcik you may try below:

for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "[id^='rlimg0_']"))):
    div = driver.find_element_by_xpath("//div[@class='xpdopen']")
    item.location
    item.click()
    wait.until(EC.staleness_of(div))

Upvotes: 2

Related Questions