Selenium implements multi-page switching

When using Selenium for automated testing or crawling data, sometimes it is necessary to handle switching between multiple pages. Here are some situations where multiple page switching may be required:

1. Open a new window/page:

When you click on a link, button or perform some action on the current page, a new window or page may open. At this point, you need to switch to a new window or page in order to perform operations there.

2. Handle the pop-up window:

Some websites may pop up new windows during operation, such as pop-up login boxes, prompt boxes, etc. In this case, you need to switch to a popup window to operate.

3. Process iframe embedded pages:

Some web pages contain iframe elements, which can be embedded in other web pages. If you need to interact with the content in the iframe, you need to switch to the page where the iframe is located.

4. Perform multiple tasks:

Sometimes you need to perform different tasks on different pages, such as filling out a form on one page, and then searching and getting results on another page. In this case, it is necessary to switch between different pages.

To handle these situations, Selenium provides some methods to switch between multiple pages:

window_handles attribute: You can use driver.window_handles to get the handle list of all currently open windows. Each window is identified by a unique handle.

switch_to.window(handle) method: You can use the driver.switch_to.window(handle) method to switch to the specified window handle. This switches the operating focus from one window to another.

Through the above method, it is possible to switch between different pages to perform the desired operation.

The following details how to use Selenium to achieve multi-page switching:

1. First, create a WebDriver object. This could be ChromeDriver, FirefoxDriver, etc.

from selenium import webdriver 
driver = webdriver.Chrome() 

2. Open the first page. Use the get() method to open the specified URL.

driver.get('http://www.example.com/page1') 

3. Find and click a link on the first page to open a new page.

link = driver.find_element_by_link_text('Open new page') 
link.click()

4. Obtain a list of all currently open window handles.

window_handles = driver.window_handles

Selenium uses the window_handles property to store all window handles currently open in the browser. Each window has a unique handle.

5. Switch to a new page.

driver.switch_to.window(window_handles[1])

Use the switch_to.window() method to switch to the specified window handle. In the example above, selecting the second window handle switches to the new page.

Now, you can work on the new page.

6. Switch back to the original page.

driver.switch_to.window(window_handles[0])

Use the switch_to.window() method to switch back to the first window handle, which is the original page.

7. Continue to perform other operations on the original page.

# 在原始页面上进行其他操作

8. Finally, close the browser.

driver.quit()

The above are the basic steps of using Selenium to realize multi-page switching. You need to use the window_handles attribute to obtain all window handles, and use the switch_to.window() method to switch window handles to achieve page switching. The specific codes and operations may vary according to the actual situation, and you can make appropriate adjustments and extensions according to your own needs.

Finally: In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free【保证100%免费】
insert image description here

Software Testing Interview Documentation

We must study to find high-paying jobs. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, and Byte, and some Byte bosses have given authoritative answers. After reading this set of interview materials, I believe everyone can find a satisfactory job.

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/m0_67695717/article/details/131806518