zoukankan      html  css  js  c++  java
  • 对话框处理

    1、思路

    使用switch_to.alert()方法定位到 alert/confirm/prompt。然后使用 text/accept/dismiss/send_keys 按需进行操做。

    • text 返回 alert/confirm/prompt 中的文字信息。

    •  accept 点击确认按钮。

    •  dismiss 点击取消按钮,如果有的话。
    • send_keys 输入值,这个 alertconfirm 没有对话框就不能用了,不然会报错。

    2、需求

    在百度设置页面,设置完成后点击“保存设置”弹出提示框。通过脚本来处理这个弹窗。

    3、代码实现

     1 #coding=utf-8
     2 
     3 from selenium import webdriver
     4 from selenium.webdriver.common.action_chains import ActionChains
     5 import time
     6 
     7 #打开百度
     8 driver = webdriver.Firefox()
     9 driver.get("http://www.baidu.com")
    10 time.sleep(3)
    11 
    12 #打开搜索设置
    13 driver.find_element_by_xpath('//*[@id="u1"]/a[8]').click()
    14 time.sleep(3)
    15 driver.find_element_by_link_text(u'搜索设置').click()
    16 time.sleep(3)
    17 
    18 #保存搜索设置,弹出确认窗体
    19 driver.find_element_by_link_text(u'保存设置').click()
    20 time.sleep(5)
    21 
    22 #获得文本信息并打印
    23 alert = driver.switch_to.alert
    24 print(alert.text)
    25 
    26 #接受警告信息
    27 alert.accept()
    28 time.sleep(5)
    29 
    30 #取消对话框(如果有的话)
    31 # alert.dismiss()
    32 #输入值(如果有的话)
    33 # alert.send_keys("xxx")
    34 #关闭浏览器
    35 driver.close()

     

  • 相关阅读:
    uva400 Unix ls
    cf641 div2 abcd
    cf619 div2 abcd
    cf620 div2 abcde
    atc160
    cf638 div2 abcd
    CodeCraft-20(Div. 2 abcd
    cf Round 621 abcd
    luogu1941 飞扬的小鸟
    UVA1601 The Morning afther Halloween
  • 原文地址:https://www.cnblogs.com/huiguniang/p/7096255.html
Copyright © 2011-2022 走看看