如何使用 Selenium WebDriver for python 在浏览器上打开一个新窗口? [英] How to open a new window on a browser using Selenium WebDriver for python?

查看:52
本文介绍了如何使用 Selenium WebDriver for python 在浏览器上打开一个新窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

  1. 切换到一个不存在的窗口,希望在找不到该窗口时打开一个新窗口:

  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)

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

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

for handle in driver.window_handles:
    driver.switch_to_window(handle)

  • 尝试模拟键盘按键

  • Attempting to simulate a keyboard key press

    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')
    

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

    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:

    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)
    

    推荐答案

    你做这样的事情怎么样

    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
    

    <小时>

    对于所有反对者:

    OP 要求打开浏览器的第二个实例很重要.".这个答案并不包含每个人的用例的所有可能要求.下面的其他答案可能适合您的特定需求.

    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.

    这篇关于如何使用 Selenium WebDriver for python 在浏览器上打开一个新窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆