In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article, "python Web Crawler selenium opens multiple windows and switch pages", so the editor summarizes the following contents to you, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article, "python Web Crawler selenium opens multiple windows and switching pages" article.
First, open Baidu.
From selenium import webdriverimport timedriver = webdriver.Chrome () driver.get ('https://www.baidu.com/')
Take Zhihu as an example, open a new interface and use the execute_script () method to execute the JavaScript code.
And call the current_url property of driver, and then view the url of the current page:
Driver.execute_script ("window.open ('https://www.zhihu.com')")print(driver.current_url))"
The current page is still Baidu. If you want to change the current page to Zhihu, you need to continue to switch it over with code.
The print handle, that is, the window_handles property of driver, outputs as follows:
Print (driver.window_handles)
Is a list of two elements, representing the two web pages opened above, Baidu and Zhihu.
Pass it into the switch_to.window () method respectively, and then print the url of the current page. The output is as follows. You can see that the page has been switched successfully.
Win1 = driver.window_handles [0] driver.switch_to.window (win1) print (driver.current_url) time.sleep (2) win2 = driver.window_handles [1] driver.switch_to.window (win2) print (driver.current_url)
About the order of handles in the list
It is recommended that it is generally best to open only one additional web page, and it is not recommended to open more web pages to switch. When there are more than two open pages, it is not easy to determine which page the elements in the handle list represent. Take the following code as an example, in the following order.
From selenium import webdriverimport timedriver = webdriver.Chrome () driver.get ('https://www.baidu.com/')# opens a new page driver.execute_script (' window.open ('https://www.zhihu.com')")driver.execute_script("window.open('https://www.lagou.com/')")driver.execute_script("window.open('https://www.jianshu.com/')")win1 = driver.window) _ handles [0] driver.switch_to.window (win1) print (driver.current_url) time.sleep (2) win2 = driver.window_handles [1] driver.switch_to.window (win2) print (driver.current_url) time.sleep (2) win3 = driver.window_handles [2] driver.switch_to.window (win3) print (driver.current_url) time.sleep (2) win4 = driver.window_handles [3] driver.switch_to.window (win4) print (driver.current_url)
The output is shown in the figure, and the first element of the handle represents the main web page interface that we opened for the first time
Then we open Zhihu, retractor and brief book in turn, but the 234 elements in the handle list represent brief book, retractor and Zhihu in turn.
It can be considered that it is the opposite of the order in which our code is written after the second element.
The above is about the content of this article on "python web crawler selenium how to open multiple windows and switch pages". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
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: 291
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.