Search code examples
pythonseleniumselenium-webdriverselenium-chromedriverwebdriverwait

Wait only one element to load in selenium python


Is there a way that I only need to wait one element to load in a site and them go to another one using selenium?

I'm crawling a site that takes too long to load everything, but the info that I need is only a text that is the first thing that load.

I'm trying something like this:

df = pd.read_csv("file.csv") 
wait =  WebDriverWait(driver, 1)

for site in df.site: 
    driver.get(site)

    search = wait.until(EC.visibility_of_any_elements_located((By.XPATH, '//div[@itemprop="recipeInstructions"]//span[@tabindex="0"]')))
    search = "".join(list(map(lambda x: x.text+"---",search)))

    if(search):
        driver.execute_script("window.stop();")

    print(search)

Solution

  • You need to wait for visibility of that specific element only.
    So, in case //div[@itemprop="recipeInstructions"]//span[@tabindex="0"] is unique locator of that specific element you can use code like this:

    wait = WebDriverWait(driver, 30)
    
    for site in df.site: 
        driver.get(site)
        text_you_want_to_get_is = wait.until(EC.visibility_of_element_located((By.XPATH, '//div[@itemprop="recipeInstructions"]//span[@tabindex="0"]'))).text