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")
    '''

  • 相关阅读:
    第11次作业
    第十次作业
    找回感觉的练习
    Tomact学习笔记
    移动端问题小计
    节流和防抖函数
    requestAnimationFrame动画封装
    svg实现渐变进度圆环
    手机端判断安卓,iso,微信
    git常用指令
  • 原文地址:https://www.cnblogs.com/bzdmz/p/10329163.html
Copyright © 2011-2022 走看看