ChromeDriver上的Time.sleep()

问题描述 投票:1回答:2

我正在使用ChromeDriver使用Python进行某些网络抓取。我的代码使用browser.find_element_by_xpath,但我必须在点击/输入之间包含time.sleep(3),因为在执行下一行代码之前,我需要等待网页加载完毕。

想知道是否有人知道最好的方法吗?也许这项功能可以在浏览器加载时立即自动执行下一行,而无需等待任意秒数?

谢谢!

python selenium selenium-chromedriver webdriverwait
2个回答
4
投票

如下所示,使用explicit wait尝试expected_conditions

进口需要:

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

然后您可以在交互之前等待元素出现。

# waiting for max of 30 seconds, if element present before that it will go on to the next line.
ele = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"xpath_goes_here")))
ele.click() # or what ever the operation like .send_keys()

这样,应用程序将动态等待,直到元素出现。如果需要,请根据您的应用程序将时间从30秒开始更新。

也可以在检查元素存在时使用不同的定位策略,例如:By.CSS_SELECTOR/By.ID/By.CLASS_NAME


0
投票

在这种情况下,我使用了一个函数来增加脚本的健壮性。例如,通过xpath查找元素:

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


def findXpath(xpath,driver):
    actionDone = False
    count = 0
    while not actionDone:
        if count == 3:
            raise Exception("Cannot found element %s after retrying 3 times.\n"%xpath)
            break
        try:
            element = WebDriverWait(driver, waitTime).until(
                    EC.presence_of_element_located((By.XPATH, xpath)))
            actionDone = True
        except:
            count += 1
    sleep(random.randint(1,5)*0.1)
    return element 

让我知道这对您有用!

© www.soinside.com 2019 - 2024. All rights reserved.