简体   繁体   English

只等待一个元素加载 selenium python

[英]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?有没有一种方法我只需要等待一个元素加载到站点中,然后使用 selenium 将它们从 go 加载到另一个元素?

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)

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:因此,如果//div[@itemprop="recipeInstructions"]//span[@tabindex="0"]是该特定元素的唯一定位器,您可以使用如下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM