Win+python+selenium Implementing Windows and Tab switching

Source: Internet
Author: User
Tags selenium python

This summary is mainly about two aspects of the need: one, in the Browser different tab tab between the time switch (colleagues used to constantly refresh the grid Crontol monitoring page); second, the implementation of opening multiple windows, and the window zoom to a certain extent, and fill the entire desktop, Refresh several pages at different intervals.

One, switch between tabs

Before this by looking for a browser plug-in tab-rotator implementation of its requirements, did not write the program implementation, the back of nothing when the online selenium data write a, the content is as follows:

#code by Www.111cn.net
From Selenium.webdriver.common.keys import keys
From selenium import Webdriver
Import time
Driver = Webdriver. Firefox ()
Driver.set_page_load_timeout (60)
Driver.implicitly_wait (15)
# First Tab
Driver.get ("https://www.baidu.com")
Oldtab = Driver.current_window_handle
Print Driver.title
Time.sleep (3)
# Second Tab
Driver.find_element_by_tag_name ("Body"). Send_keys (Keys.control + "T")
Driver.get ("http://www.111cn.net/")
Newtab = Driver.current_window_handle
Print Driver.title
Time.sleep (3)
# Go back to first Tab
Driver.find_element_by_tag_name ("Body"). Send_keys (Keys.control + keys.page_up)
Driver.switch_to_window (Oldtab)
Print Driver.title
Driver.refresh ()
Time.sleep (3)
# Go to Second Tab again
Driver.find_element_by_tag_name ("Body"). Send_keys (Keys.control + keys.page_up)
Driver.switch_to_window (Newtab)
Print Driver.title
Time.sleep (3)
Driver.close ()
This is just a simple implementation of a single cycle, if you want to implement a continuous loop, plus a while true dead loop can be. More help from selenium can be found in the official manual or in my Web disk to view PDF documents.

Two, multiple windows switch between

The code for switching between multiple windows is as follows:

#code by Www.111cn.net
Import Win32gui,win32com.client
#import Win32con
From selenium import Webdriver
Import time
def broswer (url,x,y):
Browser = Webdriver. Firefox ()
Browser.set_window_size (480, 800)
Browser.get (URL)
Browser.set_window_position (x, y)
#browser. Get (' https://www.163.com ')
#browser. Close ()
#browser. Quit ()
Def Windowenumerationhandler (hwnd, top_windows):
if u "Firefox" in Win32gui. GetWindowText (HWND). LOWER ():
Top_windows.append (hwnd, Win32gui. GetWindowText (hwnd)))
Broswer (' http://www.so.com ', 0,0)
Broswer (' http://www.sogou.com ', 400,0)
Broswer (' http://www.baidu.com ', 800,0)
if __name__ = = "__main__":
results = []
Top_windows = []
Win32gui. EnumWindows (Windowenumerationhandler, Top_windows)
"' For I in Top_windows:
if u "notepad" in I[1].lower ():
Print I
#w3hd =win32gui. FindWindow (' Mozillawindow ', i[1])
#win32gui. MoveWindow (W3HD, Max, Max, Max, True)
#0: Hiddle 1:display 2:min 3:max
#win32gui. ShowWindow (w3hd,0)
#win32gui. ShowWindow (W3HD, Win32con.sw_maximize)
Win32gui. ShowWindow (i[0],1)
Win32gui. SetForegroundWindow (I[0])
#break "
While True:
For I in Top_windows:
#if u "Firefox" in I[1].lower ():
Print I
Time.sleep (3)
Win32gui. ShowWindow (i[0],1)
Shell = Win32com.client.Dispatch ("Wscript.Shell")
Shell. SendKeys ('% ')
Win32gui. SetForegroundWindow (I[0])
Shell. SendKeys ("^{f5}", 0)
The window position and the size of the move is implemented through the Selenium module, the Win32gui module can also implement this function, specifically as follows:

Import Win32gui
# Find Status Bar window
Shelltray = Win32gui. FindWindow ("Shell_traywnd", None)
# display window
Win32gui. ShowWindow (Shelltray, 1)
# Hide Windows
Win32gui. ShowWindow (Shelltray, 0)
# change window size and position
Win32gui. MoveWindow (window, 0, 0, 1440, (), True)
#最大化窗口
hwnd = Win32gui. GetForegroundWindow ()
Win32gui. ShowWindow (hwnd, win32con.sw_maximize)
Note that the help document for the Pywin32 module is not viewable by the helper (module name) and has a separate help document.

Multi-window recorded video and selenium related documents can be obtained from the cloud disk of this website: http://pan.baidu.com/s/1c2bXGGg

The above is the cloud Community small series for your carefully prepared content, in the cloud Habitat Community blog, question and answer, the public number, people, courses and other related content, welcome to continue to use the upper right corner search button to search the document module Selenium Toggle Tab tab, Selenium Toggle tab, Selenium python window, selenium switch window, selenium multi-window switch, so you can get more relevant knowledge.

Win+python+selenium Implementing Windows and Tab switching

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.