如何使用 Selenium WebDriver for python 在浏览器上打开一个新窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17325629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 07:54:07  来源:igfitidea点击:

How to open a new window on a browser using Selenium WebDriver for python?

pythonseleniumselenium-webdriverwindow

提问by Zach Posten

I am attempting to open a new tab OR a new window in a browser using selenium for python. It is of little importance if a new tab or new window is opened, it is only important that a second instance of the browser is opened.

我正在尝试使用 selenium for python 在浏览器中打开一个新选项卡或一个新窗口。如果打开新选项卡或新窗口并不重要,重要的是打开浏览器的第二个实例。

I have tried several different methods already and none have succeeded.

我已经尝试了几种不同的方法,但都没有成功。

  1. Switching to a window that does not exist with hopes that it would then open a new window upon failure to locate said window:

    driver.switch_to_window(None)

  2. Iterating through open windows (although there is currently only one)

    for handle in driver.window_handles:
        driver.switch_to_window(handle)
    
  3. Attempting to simulate a keyboard key press

    from selenium.webdriver.common.keys import Keys
    driver.send_keys(Keys.CONTROL + 'T')
    
  1. 切换到一个不存在的窗口,希望它在找不到该窗口时打开一个新窗口:

    driver.switch_to_window(None)

  2. 遍历打开的窗口(虽然目前只有一个)

    for handle in driver.window_handles:
        driver.switch_to_window(handle)
    
  3. 尝试模拟键盘按键

    from selenium.webdriver.common.keys import Keys
    driver.send_keys(Keys.CONTROL + 'T')
    

The problem with this one in particular was that it does not seem possible to send keys directly to the browser, only to a specific element like this:

这个问题的特别之处在于,似乎不可能将密钥直接发送到浏览器,只能发送到像这样的特定元素:

driver.find_element_by_id('elementID').send_keys(Keys.CONTROL + 'T')

However, when a command such as this is sent to an element, it appears to do absolutely nothing. I attempted to locate the topmost HTML element on the page and send the keys to that, but was again met with failure:

然而,当像这样的命令被发送到一个元素时,它似乎什么都不做。我试图找到页面上最顶层的 HTML 元素并将密钥发送到该元素,但再次遇到失败:

driver.find_element_by_id('wrapper').send_keys(Keys.CONTROL + 'T')

Another version of this I found online, and was not able to verify its validity or lack thereof because I'm not sure what class/module which needs importing

我在网上找到的另一个版本,无法验证其有效性或缺乏有效性,因为我不确定需要导入哪个类/模块

act = ActionChains(driver)
act.key_down(browserKeys.CONTROL)
act.click("").perform()
act.key_up(browserKeys.CONTROL)

Something very similar with different syntax (I'm not sure if one or both of these is correct syntax)

具有不同语法的非常相似的东西(我不确定其中一个或两个是否是正确的语法)

actions.key_down(Keys.CONTROL)
element.send_keys('t')
actions.key_up(Keys.CONTROL)

采纳答案by Amey

How about you do something like this

你做这样的事情怎么样

driver = webdriver.Firefox() #First FF window
second_driver = webdriver.Firefox() #The new window you wanted to open

Depending on which window you want to interact with, you send commands accordingly

根据要与之交互的窗口,相应地发送命令

print driver.title #to interact with the first driver
print second_driver.title #to interact with the second driver


For all down voters:

对于所有反对者:



The OP asked for "it is only important that a second instance of the browser is opened.". This answer does not encompass ALL possible requirements of each and everyone's use cases. The other answers below may suit your particular need.

OP要求“ it is only important that a second instance of the browser is opened.”。这个答案并不包含每个人的用例的所有可能要求。下面的其他答案可能适合您的特定需求。

回答by Dhiraj

You can use execute_scriptto open new window.

您可以使用execute_script来打开新窗口。

driver = webdriver.Firefox()
driver.get("https://linkedin.com")
# open new tab
driver.execute_script("window.open('https://twitter.com')")
print driver.current_window_handle

# Switch to new window
driver.switch_to.window(driver.window_handles[-1])
print " Twitter window should go to facebook "
print "New window ", driver.title
driver.get("http://facebook.com")
print "New window ", driver.title

# Switch to old window
driver.switch_to.window(driver.window_handles[0])
print " Linkedin should go to gmail "
print "Old window ", driver.title
driver.get("http://gmail.com")
print "Old window ", driver.title

# Again new window
driver.switch_to.window(driver.window_handles[1])
print " Facebook window should go to Google "
print "New window ", driver.title
driver.get("http://google.com")
print "New window ", driver.title

回答by fx-kirin

I recommend to use CTRL + Ncommand on Firefox to reduce less memory usage than to create new browser instances.

我建议CTRL + N在 Firefox上使用command 来减少内存使用量,而不是创建新的浏览器实例。

import selenium.webdriver as webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()
body = browser.find_element_by_tag_name('body')
body.send_keys(Keys.CONTROL + 'n')

The way to switch and control windows has already been mentioned by Dhiraj.

Dhiraj已经提到了切换和控制窗口的方式。

回答by Vinay

driver = webdriver.Chrome()
driver.execute_script("window.open('');")
driver.get('first url')

driver.switch_to.window(driver.window_handles[1])
driver.get('second url')