Code examples for Python and Selenium to process browser windows

不言
Release: 2018-10-27 16:15:26
forward
5344 people have browsed it

This article brings you code examples about Python and Selenium processing browser windows. It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. Helps.

Last time I mentioned the browser's pop-up box processing. The browser window will also be triggered for some reason, causing the browser to open a new window. Locating a new window is similar to locating a pop-up box. You need to switch to the window before you can operate it.

Browser window related API

##quit()Close the browser##window_handle is the unique identifier of the browser window, generally called the window handle. Only through this handle can you switch windows freely. close() and quit() are also listed together, emphasizing the difference between closing the window and closing the browser.
Name Usage
switch_to.window(window_handle) Switch window
current_window_handle Current window handle
window_handles All window handles
close() Close the current window

Example

We directly use Baidu's webpage as an example. Open a new window by clicking "Beijing Public Network Security...No." at the bottom of Baidu's homepage, close the new window, and then return to Baidu home page.

We do two small tests:

    Open Baidu homepage, we first get the current window handle, after opening a new window, get the window handle again, and put the two The results obtained are printed out.
  • After closing the new window, the browser automatically switches back to the original Baidu homepage. At this time, operate the Baidu homepage
  • In order to prevent the program Interrupt, use try/except/finally to handle where necessary. Let's look at the code and results.
# coding = utf-8
from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('https://www.baidu.com')

first_window_handle = driver.current_window_handle #获取当前(百度首页)的窗口句柄
print('now handle is : ',first_window_handle) #打印百度首页窗口句柄

driver.find_element_by_id('jgwab').click() #点击打开新窗口
second_window_handle = driver.current_window_handle #再获取一次窗口句柄并打印
print('and now handle is : ', second_window_handle) 
all_handles = driver.window_handles #获取所有窗口句柄
sleep(2)
for window in all_handles:
    if window != first_window_handle:
        print('and and now handle is : ', window)
        driver.switch_to.window(window) #切换到非百度首页的窗口
        driver.close() #关闭该窗口
sleep(2)

try:
    driver.find_element_by_id('kw').send_keys('test') #关闭窗口后操作百度首页元素
    driver.find_element_by_id('su').click()
except Exception as msg:
    print('error: ', msg) #如果有错误,打印错误信息
finally:
    driver.switch_to.window(first_window_handle) #切换到百度首页后再操作
    driver.find_element_by_id('kw').send_keys('try again')
    driver.find_element_by_id('su').click()
    sleep(2)
    driver.quit()
Copy after login

Let’s take a look at the execution results:

now handle is :  CDwindow-6CF04CE58FCA5EA42EC453509FFDF773
and now handle is :  CDwindow-6CF04CE58FCA5EA42EC453509FFDF773
and and now handle is :  CDwindow-C6EDDF33CAA9BC9330585B2DCB8A664B
error:  Message: no such window: target window already closed
from unknown error: web view not found
  (Session info: chrome=69.0.3497.92)
  (Driver info: chromedriver=...
Copy after login

Looking back at the test just now, we found: 1. Even if a new window is opened, the window handle obtained is still the handle of the Baidu homepage (printed The handle is the same twice). 2. After closing the new window, the browser displays the Baidu homepage by default, but cannot operate the Baidu homepage. Instead, an error is reported: There is no window, and the target window has been closed. We must switch back to the Baidu homepage in the program before we can operate the elements of the homepage ("test" was not retrieved, but "test again" was retrieved).

Summary

What you see is not necessarily true. Except for the window displayed when the script initially opens the browser, the window displayed subsequently through a series of actions (such as opening/closing the window) does not represent Can be operated directly. Before positioning each window element, you must first use switch_to.window() to switch. The driver will not jump by itself.

The above is the detailed content of Code examples for Python and Selenium to process browser windows. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template