Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Selenium Learning: multi-window switching

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

File name: test12.py

From selenium import webdriverimport timedriver = webdriver.Chrome () file_path=' http://www.baidu.com'driver.get(file_path)# get Baidu search window handle search_windows=driver.current_window_handledriver.find_element_by_link_text ('login'). Click () driver.find_element_by_link_text ('register now'). Click () # get the handle all_handles=driver.window_handles# of all currently open windows to enter Registration window for handle in all_handles: if handlekeeper searching windows: driver.switch_to.window (handle) print ('now register windows') Driver.find_element_by_name ('userName'). Send_keys (' username') driver.close () time.sleep (2) # return to the search window for handle in all_handles: if handle = = search_windows: driver.switch_to.window (handle) print ('now search window') # close the login pop-up driver.find_element_by_xpath ('/ / * [@ id= "TANGRAM__PSP_2__titleButtons"]') .click () driver.find_element_by_xpath ("/ / input [@ id='kw']") .send_keys ("selenium") driver.find_element_by_id ('su') .click () time.sleep (2) driver.quit ()

The execution process of the above code is as follows: first, open the home page of Baidu, obtain the handle of the current window through current_window_handle, and assign the current handle value to sreach_windows

Open the pop-up box through driver.find_element_by_link_text ('login'). Click ()

Enter the registration page through driver.find_element_by_link_text ('register now'). Click ()

Get all current window handles through driver.window_handles

The first loop, if the current handle is not equal to search_window, then it must not be the home page of Baidu, but must be the registration window. After the registration operation is completed, the registration window is closed through driver.close.

The second cycle, if it is the home page of Baidu, then switch to the home page of Baidu through switch_to.window

Close the login pop-up box via driver.find_element_by_xpath ('/ * [@ id= "TANGRAM__PSP_2__titleButtons"]').

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report