zoukankan      html  css  js  c++  java
  • web自动化中的三种切换--窗口切换

    1、窗口切换

    • 获取打开的窗口句柄总数,返回的是一个列表

        handles=driver.window_handles

    • 切换到最新的窗口句柄

        driver.switch_to.window(handles[-1])

    • 操作代码如下:
     1 import time
     2 from selenium import webdriver
     3 from selenium.webdriver.support import expected_conditions as EC
     4 from selenium.webdriver.support.wait import WebDriverWait
     5 from selenium.webdriver.common.by import By
     6 # 窗口切换操作
     7 driver=webdriver.Chrome()
     8 # 访问一个网页
     9 driver.get("https://www.baidu.com")
    10 driver.maximize_window()
    11 driver.find_element_by_id('kw').send_keys("柠檬班")
    12 driver.find_element_by_id('su').click()
    13 WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH,'//a[contains(text(),"吧_百度贴吧")]')))
    14 driver.find_element_by_xpath('//a[contains(text(),"吧_百度贴吧")]').click()
    15 time.sleep(0.5)#等待0.5秒,确定点击的网页都打开
    16 # 获得打开浏览器的句柄,返回的是列表
    17 handles=driver.window_handles
    18 print(handles)
    19 driver.switch_to.window(handles[-1])
    20 # 打印当前句柄
    21 print(driver.current_window_handle)
    22 # :Args:    driver.switch_to.window(window_name)
    23 #          - window_name: The name or window handle of the window to switch to.
    24 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[@id='signstar_wrapper']//a[@title='签到']")))#等待元素出现
    25 driver.find_element_by_xpath("//div[@id='signstar_wrapper']//a[@title='签到']").click()
    26 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.ID,"TANGRAM__PSP_11__footerULoginBtn")))
    27 driver.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn').click()
    28 driver.find_element_by_id('TANGRAM__PSP_5__closeBtn').click()

    2、判断新的窗口是否已经打开使用 

      EC.new_window_is_opened(current_handles)

      操作代码如下:

      

     1 import time
     2 from selenium import webdriver
     3 from selenium.webdriver.support import expected_conditions as EC
     4 from selenium.webdriver.support.wait import WebDriverWait
     5 from selenium.webdriver.common.by import By
     6 # 窗口切换操作
     7 driver=webdriver.Chrome()
     8 # 访问一个网页
     9 driver.get("https://www.baidu.com")
    10 driver.maximize_window()
    11 driver.find_element_by_id('kw').send_keys("柠檬班")
    12 driver.find_element_by_id('su').click()
    13 WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH,'//a[contains(text(),"吧_百度贴吧")]')))
    14 # 获取打开新页面之前的窗口总的句柄
    15 handles=driver.window_handles
    16 driver.find_element_by_xpath('//a[contains(text(),"吧_百度贴吧")]').click()
    17 time.sleep(0.5)#等待0.5秒,确定点击的网页都打开
    18 # 等待新页面打开
    19 WebDriverWait(driver,20).until(EC.new_window_is_opened(handles))
    20 # 获得打开浏览器的句柄,返回的是列表
    21 handles=driver.window_handles
    22 print(handles)
    23 # 切换到新的句柄
    24 driver.switch_to.window(handles[-1])
    25 # 打印当前句柄
    26 print(driver.current_window_handle)
    27 # :Args:    driver.switch_to.window(window_name)
    28 #          - window_name: The name or window handle of the window to switch to.
    29 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[@id='signstar_wrapper']//a[@title='签到']")))#等待元素出现
    30 driver.find_element_by_xpath("//div[@id='signstar_wrapper']//a[@title='签到']").click()
    31 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.ID,"TANGRAM__PSP_11__footerULoginBtn")))
    32 driver.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn').click()
    33 driver.find_element_by_id('TANGRAM__PSP_5__closeBtn').click()
  • 相关阅读:
    SpringBoot学习笔记(14)----应用监控-HTTP方式
    SpringBoot学习笔记(13)----使用Spring Session+redis实现一个简单的集群
    SpringBoot学习笔记(12)----SpringBoot实现多个 账号轮询发送邮件
    SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用
    SpringBoot学习笔记(8)-----SpringBoot文件上传
    SpringBoot学习笔记(7)-----CORS支持解决跨域问题
    设计模式:迭代器模式(Iterator)
    设计模式:适配器模式(Adapter)
    设计模式:状态模式(State)
    设计模式:抽象工厂模式(Abstract Factory)
  • 原文地址:https://www.cnblogs.com/wsk1988/p/12696810.html
Copyright © 2011-2022 走看看