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

1586010002-jmsa.png

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.

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

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)

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值