欢迎来到魔幻小生的博客

Selenium 三种等待方式

直接等待

强制等待,线程休眠一定时间。一般在调试/演示代码时才使用。

缺点:无法准确把握需要等待的时间。设置过长浪费时间,设置过短没有作用。

time.sleep(3)

例子:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By


class TestWait:
    def setup_method(self, method):
        self.driver = webdriver.Chrome()
        self.vars = {}

    def teardown_method(self, method):
        self.driver.quit()

    def test_wait(self):
        self.driver.get("https://ceshiren.com/")
        self.driver.find_element(By.XPATH, '//*[@title="按类别分组的所有话题"]').click()
        time.sleep(3)
        self.driver.find_element(By.XPATH, '//*[@title="过去一年、一个月、一周或一天中最活跃的话题"]').click()
        time.sleep(3)
        print('hello')

隐式等待

设置一个等待时间,轮询查找(默认0.5秒)元素是否出现,如果没出现就抛出异常。在整个driver周期中都起作用,设置一次全局有效

如果元素可以定位,则继续执行接下来的操作,如果超过最大等待时间元素仍定位不到,则会报异常处理。

缺点:如果你只需要等待某个元素加载完成进行操作,但是该等待方式需要等待界面上的所有元素全部加载完毕,才会执行元素操作,从而存在因为加载整个页面元素而浪费时间问题。

self.driver.implicitly_wait(5)

例子:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By


class TestWait:
    def setup_method(self, method):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(3)

    def teardown_method(self, method):
        self.driver.quit()

    def test_wait(self):
        self.driver.get("https://ceshiren.com/")
        self.driver.find_element(By.XPATH, '//*[@title="按类别分组的所有话题"]').click()
        self.driver.find_element(By.XPATH, '//*[@title="过去一年、一个月、一周或一天中最活跃的话题"]').click()
        print('hello')

显式等待

在代码中定义等待条件,当条件发生时才继续执行代码。

程序每隔一段时间(默认0.5秒)进行条件判断,条件成立则执行下一步,否则继续等待,直到超过设置的最长时间。

WebDriverWait 类配合 until()/until_not()方法,等待条件 method 由 expected_conditions 类提供。

缺点:使用较为复杂,每个元素需要单独使用,无法全局配置。

WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)

  • driver:webdriver实例对象
  • timeout:最长超时时间,以秒为单位
  • poll_frequency:检测的间隔步长,默认为0.5s。(可不填)
  • ignored_exceptions:超时后抛出的异常信息,默认抛出NoSuchElementExeception异常。(可不填)

until/until_not(self, method, message: str = "")

until():条件成立返回True,等待结束,如果超时,抛出TimeoutException异常。

until_not():条件不成立返回True,等待结束,如果超时,抛出TimeoutException异常。

  • method:每次轮询时执行的等待条件。
  • message:轮询超时后打印的信息。(可不填)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class TestWait:
    def setup_method(self, method):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(3)

    def teardown_method(self, method):
        self.driver.quit()

    def test_wait(self):
        self.driver.get("https://ceshiren.com/")
        self.driver.find_element(By.XPATH, '//*[@title="按类别分组的所有话题"]').click()
        WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable((By.XPATH, '//*[@class="nav-item_categories categories ember-view"]')))
        self.driver.find_element(By.XPATH, '//*[@title="过去一年、一个月、一周或一天中最活跃的话题"]').click()
        print('hello')
posted @ 2025-03-17 23:25  魔幻小生  阅读(40)  评论(0)    收藏  举报