How to solve Selenium handle, multi-window problem

Sometimes when we open the browser browse the Web, when clicking on certain links page, it's not on the current page jumps directly, but rather to re-open a new tab page, in this case, to operate on a new page, You have to first switch the window. Uniquely identifies the acquisition window is represented by a handle, so just need to switch handle, we can on multiple pages smooth operation of the.

First, the understanding of multi-window

1, here to go to the market network, for example, go to the market open , click on the job, you will find will re-open a window;

 


image

Second, get the current window handle

1. element has attributes, the browser window actually have the property, but you can not see, the properties of the browser window with the handle (handle) to identify.

2. The human operator then, by eyes, different identification window, click switch. But the scripts do not have eyes, it does not know which window you want to operate, this time can only handle to judge.

3. Get a handle to the current page: browser.current_window_handle

 


image

4. Perform results

 


image

Third, access to all window handle

1, get all window handle: brows.window_handles

 


image

2, the results

 


image

Fourth, switching handle

method one:

1. The cycle is equal to the determined Home handle;

2. If unequal, that is the handle to the new page;

3. After the new page handle acquired, can be switched to a new page is opened;

4. Print the title of the new page, to see if the switch was successful.

 


image

Results of the

 


image

Method Two:

Direct access to the list of data which all_h second hand value: all_h [i]

 


image

Results of the

 


image

Fifth, close the new window to return to the original window

1. Open a new page, in fact, just want to jump right to verify a new page, where you can make a simple validation, verification acquiring title of the current page;

2. Verify After cutting off the new window;

3. Cut back the handle to the front page;

4. Print the handle of the current page, to see whether to switch to the home page.

 


image

Results of the

 


image

Sixth, reference code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File    : handles.py
# @Software: PyCharm
import time
from selenium import webdriver
url = 'http://cd.ganji.com/'
browser = webdriver.Firefox()
browser.get(url)
time.sleep(5)
# 获取当前页面的窗口句柄
handle = browser.current_window_handle
print(handle)
# 获取所有窗口句柄
browser.find_element_by_link_text('成都招聘').click()
handles = browser.window_handles
print(handles)
# 方法一:判断句柄是否与首页相等
for i in handles:
    if i != handle:
        browser.switch_to.window(i)
        print(browser.title)
        browser.close()
        browser.switch_to.window(handle)
        print(browser.title)
# 方法二:直接获取list列表里面的值,取值handles[i]
# browser.switch_to.window(handles[0])
# print(browser.title)
# 退出
browser.quit()

Guess you like

Origin www.cnblogs.com/jiachangwei/p/12148409.html