zoukankan      html  css  js  c++  java
  • Python3---Selenium---Window窗口切换

    Python3---Selenium---Window窗口切换

    2020-04-10-17:29:55

      Selenium通过“handle”(窗口句柄)来定位网页窗口。窗口句柄简单说可以认为是窗口的ID。

      使用Webdriver对象的switch_to属性的 window方法,来切换窗口:

      switch_to.window(handle)

    E.G

    from selenium import webdriver
    import time
    
    driver = webdriver.Chrome('D:chromedriver_win32chromedriver.exe')
    driver.implicitly_wait(10)
    
    driver.get('http://cdn1.python3.vip/files/selenium/sample3.html')
    
    #保存当前窗口的句柄
    mainWindow = driver.current_window_handle
    
    #点击打开新的窗口链接
    link = driver.find_element_by_tag_name('a')
    link.click()
    
    #打印出当前窗口的标题栏
    print('查看第一次标题栏:',driver.title)
    
    #切换新的窗口
    #handle 窗口句柄,window_handles属性,列表对象,里面包括了当前浏览器里面所有的窗口句柄。
    for handle in driver.window_handles:
        #切换窗口
        driver.switch_to.window(handle)
        #if 语句判断是不是我们需要的窗口
        #print(handle)
        if 'Bing' in driver.title:
            #如果是我们需要的窗口,则跳出循环
            break
    print('切换窗口标题:',driver.title)
    
    driver.switch_to.window(mainWindow)
    
    print('切换回主页面标题:',driver.title)
    time.sleep(2)
    
    
    driver.close()
  • 相关阅读:
    html php插入百度地图定位
    thinkphp验证功能(部分)
    thinkphp用ajax注册及检测个人见解
    文件系统处理_下
    文件系统处理
    jQuery ajax
    jquery(复选框全选)
    jquery(鼠标)
    找房子(数据库应用)
    php基础题
  • 原文地址:https://www.cnblogs.com/aaron456-rgv/p/12674817.html
Copyright © 2011-2022 走看看