深圳网站建设排行,网站建设开发的条件,长春火车站什么时候解封,微信网站开发服务前言系统弹窗这个是很常见的场景#xff0c;有时候它不弹出来去操作的话#xff0c;会抛异常。那么又不知道它啥时候会出来#xff0c;那么久需要去判断弹窗是否弹出了。一、判断alert源码分析class alert_is_present(object): Expect an alert to be pre…前言系统弹窗这个是很常见的场景有时候它不弹出来去操作的话会抛异常。那么又不知道它啥时候会出来那么久需要去判断弹窗是否弹出了。一、判断alert源码分析class alert_is_present(object): Expect an alert to be present.判断当前页面的alert弹窗def __init__(self):passdef __call__(self, driver):try:alert driver.switch_to.alertalert.textreturn alertexcept NoAlertPresentException:return False1.这个类比较简单初始化里面无内容2.__call__里面就是判断如果正常获取到弹出窗的text内容就返回alert这个对象(注意这里不是返回Ture)没有获取到就返回False二、实例操作1.前面的操作步骤优化了下为了提高脚本的稳定性确保元素出现后操作2.实现步骤如下这里判断的结果返回有两种没找到就返回False找到就返回alert对象3.先判断alert是否弹出如果弹出就点确定按钮accept()三、参考代码# coding:utf-8from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.support.select import Selectfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver webdriver.Firefox()url https://www.baidu.comdriver.get(url)mouse WebDriverWait(driver, 10).until(lambda x: x.find_element(link text, 设置))ActionChains(driver).move_to_element(mouse).perform()WebDriverWait(driver, 10).until(lambda x: x.find_element(link text, 搜索设置)).click()# 选择设置项s WebDriverWait(driver, 10).until(lambda x: x.find_element(id, nr))Select(s).select_by_visible_text(每页显示50条)# 点保存按钮js ‘document.getElementsByClassName(prefpanelgo)[0].click();‘driver.execute_script(js)# 判断弹窗结果 交流QQ群: 232607095result EC.alert_is_present()(driver)if result:print result.textresult.accept()else:print alert 未弹出原文http://www.cnblogs.com/caoj/p/7814165.html