zoukankan      html  css  js  c++  java
  • pythonselenium 弹出框处理

    弹出框有两种:页面弹出框(可定位元素能操作)、Windows弹出框(不能直接定位)

    一、页面弹出框

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

      如: 

     1 driver = webdriver.Chrome()
     2 driver.get("https://www.baidu.com")
     3 driver.maximize_window()
     4 #点击百度登录按钮
     5 driver.find_element_by_xpath('//*[@id="u1"]//a[@name="tj_login"]').click()
     6 
     7 #等待百度登录弹出框中 要出现的元素可见
     8 ele_id = "TANGRAM__PSP_10__footerULoginBtn"
     9 param = (By.ID,ele_id)
    10 #元素可见时,再进行后续操作
    11 WebDriverWait(driver,10).until(EC.visibility_of_element_located(param))
    12 
    13 driver.find_element_by_id(ele_id).click()
    14 time.sleep(5)
    15 driver.quit()

    二、Windows弹出框

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

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

      accept() 确定

      dismiss() 取消

      text 获取弹出框里面的内容

      send_keys(keysToSend) 输入字符串

      如: 

     1 #1:定位alert弹出框
     2 #点击页面元素,触发alert弹出框
     3 driver.find_element_by_xpath('//*[@id="alert"]').click()
     4 time.sleep(3)
     5 #等待alert弹出框可见
     6 WebDriverWait(driver,20).until(EC.alert_is_present())
     7 
     8 #从html页面切换到alert弹框 ,是个属性且有返回值需要接收
     9 alert = driver.switch_to.alert
    10 #获取alert的文本内容-属性
    11 print(alert.text)
    12 #接受--选择“确定”
    13 alert.accept()
    14 
    15 #2:定位confirm弹出框
    16 driver.find_element_by_xpath('//*[@id="confirm"]').click()
    17 time.sleep(3)
    18 WebDriverWait(driver,20).until(EC.alert_is_present())
    19 alert =driver.switch_to.alert
    20 print(alert.text)
    21 # 接受--选择“取消”--方法
    22 alert.dismiss()
    23 
    24 
    25 #3:定位prompt弹出框
    26 driver.find_element_by_id("prompt").click()
    27 time.sleep(3)
    28 WebDriverWait(driver,20).until(EC.alert_is_present())
    29 alert =driver.switch_to.alert
    30 alert.send_keys("jaja")
    31 time.sleep(5)
    32 print(alert.text)
    33 # alert.dismiss()
    34 alert.accept()

     

  • 相关阅读:
    CF 118E Bertown roads 桥
    hdu 3917 Road constructions 最大权闭合子图
    hdu 4714 Tree2cycle 树形经典问题
    POJ 2516 Minimum Cost 最小费用流
    POJ 3921 Destroying the bus stations 沿着最短路迭代加深搜索
    POJ 3422 Kaka's Matrix Travels K取方格数
    BZOJ 3083: 遥远的国度 dfs序,树链剖分,倍增
    hdu 4010 Query on The Trees LCT
    poj 2455 Secret Milking Machine 二分+最大流 sap
    定制标记---简单标记处理器
  • 原文地址:https://www.cnblogs.com/simran/p/9240917.html
Copyright © 2011-2022 走看看