Selenium operating IE11-unable to open a new window

surroundings:

  • windown 7 (10) / IE11
  • python 3.7.1 / selenium 3.14.1

During this time, there is a small task that requires IE to open multiple URLs. Selenium has a lot of pitfalls in using IE11, which wastes a lot of time. Please refer to the setting: webdriver starts IE11 .

In the end, there is still one problem: selenium operates IE11, no way to open a new window. window_handles is always 1, which is the one opened by the webdriver object. Even if you manually open the link in a new window, window_handls is still 1, so you can't manage the IE11 window. In firefox, it is possible to open a new window, whether it is automatic or manual, you can use the following code to switch the management window. (Chrome not tested)

from selenium import webdriver

driver = webdriver.Firefox()
......
print(len(driver.window_handles)) # 显示打开的窗口数
print(driver.window_handles) # 显示各窗口句柄,列表
for handle in driver.window_handles
    driver.switch_to.window(handle) # 切换窗口
    print(driver.title) # 显示页面标题

The following are two ways to introduce new windows in the network:

  1. Run js statements through driver.execute_script()(. For example: driver.execute_script('window.open(" http://www.baidu.com ”);')
  2. The same is to run the js statement through driver.execute_script()(. Add an a element to the opened webpage, and then use to get the a element, and use click() to achieve a new window. But the automatic click() in IE11 is always unsuccessful , You have to manually go to the point to succeed (this loses the meaning of automation).

Later, it was impossible, only to create multiple webdrivers to open multiple windows. But this is still not a good way.

Guess you like

Origin blog.csdn.net/qq_41090453/article/details/84586422