其他分享
首页 > 其他分享> > Web自动化 -- 显式等待高级使用

Web自动化 -- 显式等待高级使用

作者:互联网

显示等待原理

直接看代码原理:

 

 

显示等待-excepted_conditions

from selenium.webdriver.support.ui.WebDriverWait import WebDriverWait
from selenium.webdriver.support.excepted_conditions import excepted_conditions

def wait_until():
    driver = webdriver.Chrome()
    driver.get("https://dev.vip.ceshiren.com/#/ui_study")
    WebDriverWait(driver, 10).until(
        expected_conditions.element_to_be_clickable(
            (By.CSS_SELECTOR, '#success_btn')))
    driver.find_element(By.CSS_SELECTOR, "#success_btn").click()

常见 excepted_conditions

 
类型 示例方法
element element_to_be_clickable()
visibility_of_element_located()
url url_contains()
title title_is()
frame frame_to_be_available_and_switch_to_it(locator)
alert alert_is_present()

https://www.selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html?highlight=expected

显式等待-个人封装条件

显式等待-个人封装条件

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait


class TestWebdriverWait:

    driver = webdriver.Chrome()

    driver.maximize_window()
    driver.implicitly_wait(5)
    driver.get("https://vip.ceshiren.com/#/ui_study")
    def teardown(self):
        self.driver.quit()
    def test_webdriver_wait(self):
        # 解决的问题:有的按钮点击一次没有反应,可能要点击多次,比如企业微信的添加成员
        # 解决的方案:一直点击按钮,直到下个页面出现,封装成显式等待的一个条件
        def muliti_click(button_element,until_ele):
            # 函数封装
            def inner(driver):
                # 封装点击方法
                driver.find_element(By.XPATH,button_element).click()
                return driver.find_element(By.XPATH,until_ele)
            return inner
        time.sleep(5)
        # 在限制时间内会一直点击按钮,直到展示弹框
        WebDriverWait(self.driver,10).until(muliti_click("//*[text()='点击两次响应']","//*[text()='该弹框点击两次后才会弹出']"))
        time.sleep(5)

标签:Web,webdriver,--,selenium,driver,element,显式,import,conditions
来源: https://www.cnblogs.com/manshuoli/p/16368546.html