怎么在python中使用硒处理弹出框

  介绍

怎么在python中使用硒处理弹出框?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

python是什么意思

python是一种跨平台的,具有解释性,编译性,互动性和面向对象的脚本语言,其最初的设计是用于编写自动化脚本,随着版本的不断更新和新功能的添加,常用于用于开发独立的项目和大型项目。

<强>一、页面弹出框

等待弹出框出现之后,定位弹出框,操作其中元素

如:

driver =, webdriver.Chrome ()   driver.get (“https://www.baidu.com")   driver.maximize_window ()   #点击百度登录按钮   driver.find_element_by_xpath (& # 39;//* [@ id=皍1"]//[@ name=皌j_login"] & # 39;) .click ()      #等待百度登录弹出框中,要出现的元素可见   时间=ele_id “TANGRAM__PSP_10__footerULoginBtn"   param =, (By.ID ele_id)   #元素可见时,再进行后续操作   WebDriverWait(司机,10)除非(EC.visibility_of_element_located(参数)      driver.find_element_by_id (ele_id) .click ()   time . sleep (5)   driver.quit ()

<强>二、窗口弹出框

使用driver.switch_to.alert 切换到Windows弹出框

警告类提供了一系列操作方法:

<李>

接受()确定

<李>

把()取消

<李>

文本()获取弹出框里面的内容

<李>

send_keys (keysToSend)输入字符串

如:

# 1:定位警报弹出框   #点击页面元素,触发警报弹出框   driver.find_element_by_xpath (& # 39;//* [@ id=癮lert"] & # 39;) .click ()   time . sleep (3)   #等待警报弹出框可见   WebDriverWait(司机,20)除非(EC.alert_is_present ())      #从html页面切换到警报弹框,   alert =driver.switch_to.alert   #获取警报的文本内容   打印(alert.text)   #接受,选择”确定”   alert.accept ()      # 2:定位确认弹出框   driver.find_element_by_xpath (& # 39;//* [@ id=癱onfirm"] & # 39;) .click ()   time . sleep (3)   WebDriverWait(司机,20)除非(EC.alert_is_present ())   alert =driver.switch_to.alert   打印(alert.text)   #,接受,选择“取消”   alert.dismiss ()         # 3:定位提示弹出框   driver.find_element_by_id (“prompt") .click ()   time . sleep (3)   WebDriverWait(司机,20)除非(EC.alert_is_present ())   alert =driver.switch_to.alert   alert.send_keys (“jaja")   time . sleep (5)   打印(alert.text)   #,alert.dismiss ()   alert.accept ()

<强>实例

首先复制下列的html代码,保存为测试。html到与脚本相同的文件夹下。这个html文件包含三个按钮,点击后会弹出三种不同的弹出框,另外还有一个文字区域,显示刚才的动作。

& lt; ! doctype  html>   & lt; head>   & lt;才能title>警报,confirm 以及prompt   & lt;才能script 类型=& # 39;text/javascript # 39;比;   ,,,function  myFunctionAlert () {   ,,,,,window.alert(& # 39;却;能够is  an 警报,it  has  a  confirm 按钮# 39;)   ,,,,,. getelementbyid(& # 39;行动# 39;).value =, & # 39;你just  clicked  confirm  button  of 警报()& # 39;   ,,,}      ,,,function  myFunctionConfirm () {   ,,,,,var  result =, window.confirm(& # 39;却;能够is  a 证实,it  has  a  confirm  button 以及a  cancel 按钮# 39;)   ,,,,,如果(result ==, true) {   ,,,,,,,. getelementbyid(& # 39;行动# 39;).value =, & # 39;你just  clicked  confirm  button  of 确认()& # 39;   ,,,,,}else 如果(==result 假){   ,,,,,,,. getelementbyid(& # 39;行动# 39;).value =, & # 39;你just  clicked  cancel  button  of 确认()& # 39;   ,,,,,其他}{   ,,,,,,,document.getElementsById(& # 39;行动# 39;).value =, & # 39;你did 没有# 39;   ,,,,,}   ,,,}      ,,,function  myFunctionPrompt () {   ,,,,,var  result =, window.prompt(& # 39;却;能够is  a 提示,it  has  an  input 以及two 按钮# 39;)   ,,,,,如果(result ==, null) {   ,,,,,,,. getelementbyid(& # 39;行动# 39;).value =, & # 39;你just  clicked  cancel  button  of 提示()& # 39;   ,,,,,}else 如果(result ==, & # 39; & # 39;) {   ,,,,,,,. getelementbyid(& # 39;行动# 39;).value =, & # 39;你just  input  nothing 以及clicked  confirm  button  of 提示()& # 39;   ,,,,,其他}{   ,,,,,,,. getelementbyid(& # 39;行动# 39;).value =, & # 39;你just  input  \ & # 39; & # 39;, +, result  +, & # 39; \ & # 39;,以及clicked  confirm  button  of  promt () & # 39;   ,,,,,}   ,,,}   & lt;才能/script>   & lt;才能body>   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在python中使用硒处理弹出框