如何让硒等待多个元素加载

在继续屏幕抓取之前,我正在使用以下代码等待加载所有 4 个元素;但是,代码并没有等待所有 4 个,也没有抛出超时错误——它只是继续,我在尚未加载的元素上收到错误。


我缺少什么让 Selenium 等到所有四个元素都存在后再继续?


        CSSSelector1_toWaitOn = "#div1 table tbody tr td"

        CSSSelector2_toWaitOn = "#div2 table tbody tr:nth-child(5) td"

        CSSSelector3_toWaitOn = "#div3 table tbody tr:nth-child(5) td"

        CSSSelector4_toWaitOn = "#div4 table tbody tr td"



        browser.get(url)

        browser_delay = 15  # seconds

        try:

            WebDriverWait(browser, browser_delay).until(expected_conditions and (

                expected_conditions.presence_of_element_located((By.CSS_SELECTOR, CSSSelector1_toWaitOn)) and

                expected_conditions.presence_of_element_located((By.CSS_SELECTOR, CSSSelector2_toWaitOn)) and

                expected_conditions.presence_of_element_located((By.CSS_SELECTOR, CSSSelector3_toWaitOn)) and

                expected_conditions.presence_of_element_located((By.CSS_SELECTOR, CSSSelector4_toWaitOn))))

        except TimeoutException:

            print("Selenium timeout")```


慕村225694
浏览 113回答 3
3回答

白板的微信

WebDriverWait.until期望可调用对象。这是来自其来源的实际片段:while True:    try:        value = method(self._driver)        if value:            return value所有expected_contiditions 都是可调用对象。因此,在这种情况下,您需要编写它们,如下所示应该可以工作。class composed_expected_conditions:    def __init__(self, expected_conditions):        self.expected_conditions = expected_conditions    def __call__(self, driver):        for expected_condition in self.expected_conditions:            if not expected_condition(driver):                return False            return True并将其传递给untilconditions = [    expected_conditions.presence_of_element_located((By.CSS_SELECTOR, CSSSelector1_toWaitOn)),     expected_conditions.presence_of_element_located((By.CSS_SELECTOR, CSSSelector2_toWaitOn)),    expected_conditions.presence_of_element_located((By.CSS_SELECTOR, CSSSelector3_toWaitOn)),    expected_conditions.presence_of_element_located((By.CSS_SELECTOR, CSSSelector4_toWaitOn)),]WebDriverWait(browser, browser_delay).until(composed_expected_conditions(conditions))

叮当猫咪

该方法presence_of_element_located(locator)仅检查元素是否存在于 DOM 中。这并不意味着元素可以交互。此外,搜索过程找到给定的所有元素locator并返回第一个。请检查该元素是否有效、可用且具体。如果列表中有多个元素,请确保您的定位器足够具体以找到单个元素。

牧羊人nacy

与其尝试将它们全部组合成一个等待,不如为每个单独等待。...try:    wait = WebDriverWait(browser, browser_delay)    wait.until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, CSSSelector1_toWaitOn))    wait.until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, CSSSelector2_toWaitOn))    wait.until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, CSSSelector3_toWaitOn))    wait.until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, CSSSelector4_toWaitOn))))except TimeoutException:    print("Selenium timeout")请注意,Selenium 中有 3 个级别的元素交互:present - 元素在 DOM 中。如果您尝试单击或从现有(但不可见)元素等获取文本,ElementNotInteractable则会引发异常。可见 - 元素在 DOM 中并且可见(例如不是不可见,显示:无等)clickable - 元素可见且已启用。在大多数情况下,这只是……它是可见的吗?特殊情况是标记为禁用的 INPUT 按钮等元素。使用 CSS 设置为禁用(灰显)的元素不被视为禁用。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python