zoukankan      html  css  js  c++  java
  • 切换窗口

    在页面操作过程中有时候点击某个链接会弹出新的窗口,这时就需要主机切换到新打开的窗口上进行操作。

    WebDriver提供了switch_to.window()方法,可以实现在不同的窗口之间切换。 以百度首页和百度注册页为例,在两个窗口之间的切换如下图。

    from selenium import webdriver
    import time
    
    driver = webdriver.Firefox()
    driver.implicitly_wait(10)
    driver.get("http://www.baidu.com")
    
    # 获得百度搜索窗口句柄
    sreach_windows = driver.current_window_handle
    
    driver.find_element_by_link_text('登录').click()
    driver.find_element_by_link_text("立即注册").click()
    
    # 获得当前所有打开的窗口的句柄
    all_handles = driver.window_handles
    
    # 进入注册窗口
    for handle in all_handles:
        if handle != sreach_windows:
            driver.switch_to.window(handle)
            print('now register window!')
            driver.find_element_by_name("account").send_keys('username')
            driver.find_element_by_name('password').send_keys('password')
            time.sleep(2)
            # ……
    
    
    driver.quit()

    在本例中所涉及的新方法如下:

    • current_window_handle:获得当前窗口句柄。

    • window_handles:返回所有窗口的句柄到当前会话。

    • switch_to.window():用于切换到相应的窗口,与上一节的switch_to.frame()类似,前者用于不同窗口的切换,后者用于不同表单之间的切换。

  • 相关阅读:
    取模 分数取模 快速幂 费马小定理
    “科林明伦杯”哈尔滨理工大学第十届程序设计竞赛 部份签到题
    shell 脚本
    pyhcl语法
    数据库实验1 SQL
    杂七杂八 Ubuntu Linux
    Kattis, Kattis 的一些问题题解
    Meow Factor 【暴力】
    解决 Eclipse 项目有红感叹号的方法
    RuntimeException与CheckedException
  • 原文地址:https://www.cnblogs.com/zuoyou1223/p/11881704.html
Copyright © 2011-2022 走看看