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

How to switch the python web page window

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to switch Python web page windows, Xiaobian thinks it is quite practical, so share it for everyone to make a reference, I hope you can gain something after reading this article.

When doing web automation, there are times when an element that opens a new window and can't find another window in the current window appears, and window switching is needed.

description

The premise of window switching is to trigger a new window, a new window (usually using a handle) and get the handle of the window.

2. diver.window_handles Gets all handles of the window, has a return value, and needs a variable to receive.

Returns a list in which the most recently opened window handle is the last value in the list.

switching window

diver.switch_to.window

examples

from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium. webdriver.support import expected_conditions as ECimport time#Open a session diver = webdriver.Chrome()#Full screen diver.maximize_window()diver.implicitly_wait(30) try: #Visit Baidu link diver.get("https://www.baidu.com") WebDriverWait(diver, 20).until(EC.visibility_of_element_located((By.ID, "kw"))) #Wait for text box to be visible diver.find_element_by_id("kw").send_keys("Baidu Post Bar") #Input Content WebDriverWait(diver, 20).until(EC.visibility_of_element_located((By.ID, "su"))) #Wait for hundreds to be visible diver.find_element_by_id("su").click() #Click WebDriverWait(diver, 20).until( EC.visibility_of_element_located((By.XPATH, '//a[text()="ba- "$>'))) diver.find_element_by_xpath ('//a[text()="ba- "]').click() #Click time.sleep(3) handles_list = diver.window_handles print(handles_list)#Get handles for all windows diver.switch_to.window(handles_list[-1]) #Switch to last window--Switch to new html page #Waiting for Baidu Post Bar to be visible WebDriverWait(diver, 20).until( EC.visibility_of_element_located(( By.ID,"tab_picture"))) #Wait for picture button to be visible diver.find_element_by_id("tab_picture").click() time.sleep(3) #quit diver.quit()except Exception as e: #quit diver.quit() raise e about "python web page window how to switch" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

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

Development

Wechat

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

12
Report