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

    #coding=utf-8
    #多窗口:如点了某个链接后,会再多打开一个新窗口,即新标签页
    from selenium import webdriver
    driver = webdriver.Firefox()
    driver.implicitly_wait(10)
    driver.get("http://www.baidu.com")

    #获得当前窗口句柄
    search_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 !=search_windows:
    #如果当前窗口不是百度首页的句柄,则切换到注册句柄
    driver.switch_to_window(handle)
    print ('now register window!')
    zhuce_windows = driver.current_window_handle
    driver.find_element_by_name("account").send_keys('username')
    driver.find_element_by_name("password").send_keys('password')


    #返回搜索窗口
    for handle in all_handles:
    if handle == search_windows:
    driver.switch_to_window(handle)
    print ("now resarch window")

    #如果当前打开的不止两个窗口,则需要在每个窗口打开的时候,获取一次句柄,要切换到任何一个窗口,只需要再switch一下
    #如这个是获取注册页的句框zhuce_windws = driver.current_window_handle
    #切换时只要driver.switch_to_window(zhuce_windows) ,或者使用for +and进行循环

    '''
    for handle in all_handles:
    if handle != search_windows and handle !=zhuce_windows
    driver.switch_to_window(handle)
    print ("now is the thirds window")
    '''

  • 相关阅读:
    SPA项目开发之动态树以及数据表格和分页
    SPA项目开发之首页导航左侧菜单栏
    SPA项目开发之登录
    使用vue-cli搭建spa项目
    Splay 平衡树
    主席树(可持久化线段树 )
    P3195 [HNOI2008]玩具装箱TOY
    P2962 [USACO09NOV]灯Lights
    【hdu4405】AeroplaneChess
    HDU3853:LOOPS
  • 原文地址:https://www.cnblogs.com/bzdmz/p/10329163.html
Copyright © 2011-2022 走看看