selenium中的鼠标和键盘事件被封装在actionchains类中,使用方法:

actionchains(driver).click(btn).perform()

actionchains中常用方法:

示例网站:http://sahitest.com/demo

示例场景:打开sahi tests页面,点击“alert test”页面,鼠标点击页面中“click for alert”按钮,弹出警告提示框,判断页面是否存在alert,如存在则切换到警告框,并获取警告文本信息,之后点警告框中的确定按钮。

示例脚本:

from selenium import webdriver
from time import sleep
from selenium.webdriver.support.wait import webdriverwait
from selenium.webdriver.support import expected_conditions as ec  
class testwaitcondition(object):
    def setup(self):
        self.driver = webdriver.chrome()
        self.driver.get("https://sahitest.com/demo/")
 
    def test_waitcondition(self):
        #点页面上的alert test
        self.driver.find_element_by_xpath("/html/body/table/tbody/tr/td[3]/a[1]").click()
        #鼠标点击页面中“click for alert”按钮
        self.driver.find_element_by_name("b1").click()
 
        wait = webdriverwait(self.driver,2)
        #等待并判断警告框是否存在
        wait.until(ec.alert_is_present())
        #切换到alert页面
        alert = self.driver.switch_to.alert
        #获取并打印警告框中文本
        print(alert.text)
        #点弹出警告框中的确定
        alert.accept()
        self.driver.quit()

运行结果:

以上:来自极客时间课程:selenium自动化测试学习总结。

以上就是python自动化测试selenium核心技术等待条件教程的详细内容,更多关于selenium等待条件教的资料请关注www.887551.com其它相关文章!