Home>Article>Backend Development> Code examples for Python and Selenium to process browser windows
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.
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 |
Close the browser |
Example
We do two small tests:
# 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()
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=...
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
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!