zoukankan      html  css  js  c++  java
  • selenium获取多窗口句柄并一切换至原窗口句柄(三个窗口)

    网上有很多是selenium基于python来获取两个窗口句柄与切换,本文实现用python+selenium获取多窗口句柄并一一切换至原窗口句柄(三个窗口),且在每个窗口下进行一个搜索或翻译,然后截图。

    代码如下:

     1 # coding=utf-8
     2 from selenium import webdriver
     3 import time
     4 
     5 driver = webdriver.Chrome()
     6 driver.maximize_window() # 窗口最大化
     7 
     8 driver.get('https://www.baidu.com') # 在当前浏览器中访问百度
     9 time.sleep(2)
    10 #print(driver.current_window_handle)# 输出当前窗口句柄(百度)
    11 frist_handle = driver.current_window_handle
    12 
    13 # 新开一个窗口,通过执行js来新开一个窗口,访问搜狗
    14 js='window.open("https://www.sogou.com");'
    15 driver.execute_script(js)
    16 
    17 # 再新开一个窗口,通过执行js来新开一个窗口,访问有道
    18 js='window.open("http://www.youdao.com/");'
    19 driver.execute_script(js)
    20 
    21 handles = driver.window_handles # 获取当前窗口句柄集合(列表类型)
    22 print(handles) # 输出句柄集合
    23 
    24 for handle in handles:# 切换窗口(切换到有道)
    25     if handle != frist_handle:
    26         driver.switch_to_window(handle)
    27         #print(driver.current_window_handle)  # 输出当前窗口句柄(有道)
    28         driver.find_element_by_id("translateContent").send_keys("selenium")  #有道翻译selenium
    29         driver.find_element_by_css_selector("button").click()
    30         #driver.find_element_by_css_selector("[data-rlog='search-popup-close-win']").click()
    31         driver.find_element_by_css_selector("[class='close js_close']").click()  #关闭弹窗
    32         driver.get_screenshot_as_file("D:windows\youdao.jpg")  # 截图  可自定义截图后的保存位置(D:windows)和图片命名(youdao.jpg)
    33         time.sleep(5)
    34         break
    35 driver.close() #关闭当前窗口(有道)
    36 
    37 for handle in handles:# 切换窗口(切换到搜狗)
    38     if handle != frist_handle:
    39         driver.switch_to_window(handles[-1]) #此时只剩两个句柄,取最后一个
    40         #print(driver.current_window_handle)  # 输出当前窗口句柄(搜狗)
    41         driver.find_element_by_id("query").send_keys("selenium")  #搜狗搜索selenium
    42         driver.find_element_by_id("stb").click()
    43         time.sleep(2)  #等待2s为了截完整搜索结果图
    44         driver.get_screenshot_as_file("D:windows\sougou.jpg")  # 截图  可自定义截图后的保存位置和图片命名
    45         time.sleep(5)
    46         break
    47 driver.close() #关闭当前窗口(搜狗)
    48 
    49 #driver.switch_to_window(frist_handle) #切换回百度窗口
    50 driver.switch_to_window(handles[0]) #切换回百度窗口
    51 driver.find_element_by_id("kw").send_keys("selenium")  #百度搜索selenium
    52 driver.find_element_by_id("su").click()
    53 time.sleep(2) #等待2s为了截完整搜索结果图
    54 driver.get_screenshot_as_file("D:windows\baidu.jpg")  #截图  可自定义截图后的保存位置和图片命名
    55 time.sleep(5)
    56 driver.quit() #退出浏览器

    截图如下:

    ----------------------------------------------------------------------------------------

    talk is cheap , show me the code.

  • 相关阅读:
    VS2010 自动跳过代码现象
    Reverse Linked List II 【纠结逆序!!!】
    Intersection of Two Linked Lists
    Linked List Cycle II
    Remove Nth Node From End of List 【另一个技巧,指针的指针】
    Swap Nodes in Pairs
    Merge Two Sorted Lists
    Remove Duplicates from Sorted List
    Linked List Cycle
    Dungeon Game
  • 原文地址:https://www.cnblogs.com/chenshengkai/p/11297998.html
Copyright © 2011-2022 走看看