简体   繁体   English

控制硒中的Firefox标签

[英]Controlling firefox tabs in selenium

According to the window_handles documentation : 根据window_handles 文档

window_handles window_handles

Returns the handles of all windows within the current session. 返回当前会话中所有窗口的句柄。

But, I cannot see the new handle appearing in window_handles list after opening a new tab: 但是,打开新标签页后,我看不到新的句柄出现在window_handles列表中:

>>> from selenium import webdriver
>>> from selenium.webdriver.common.keys import Keys
>>>
>>> driver = webdriver.Firefox()
>>> driver.get("http://stackoverflow.com/")
>>> driver.window_handles
[u'{caca92e1-521e-9b4d-9374-00af0ae7d384}']
>>>
>>> # open a new tab
>>> driver.find_element_by_tag_name("body").send_keys(Keys.COMMAND + 't')
>>> driver.window_handles
[u'{caca92e1-521e-9b4d-9374-00af0ae7d384}']

As you can see, window_handles has the same value, but I see 2 tabs opened in the browser. 如您所见, window_handles具有相同的值,但是我看到在浏览器中打开了2个选项卡。 Is it something I am doing wrong? 我做错了什么吗? If yes, how should I obtain the handle of the new tab? 如果是,应该如何获取新标签的句柄?

Using: 使用方法:

  • selenium 2.44.0 (latest) 硒2.44.0(最新)
  • firefox 35.0 (latest) Firefox 35.0(最新)
  • python 2.7.6 python 2.7.6

Note that if I would make a similar thing in Chrome, window_handles would show 2 handles: 请注意,如果我要在Chrome中进行类似操作,则window_handles将显示2个手柄:

>>> driver = webdriver.Chrome()
>>> driver.get("http://stackoverflow.com/")
>>> driver.execute_script('window.open("about:blank", "_blank");')
>>> driver.window_handles
[u'CDwindow-9458E5DB-D5ED-496C-BEE7-2FA468F3DF42', u'CDwindow-04C0FBBC-C418-465B-B6AF-F72B288B45C6']

Only the top level browser window has an HWND. 仅顶级浏览器窗口具有HWND。 Tabs don't have their own HWNDs. 选项卡没有自己的HWND。 For more clarification refer here . 有关更多说明,请参见此处

Selenium have a Handle for Windows not for tabs. Selenium有一个Windows句柄,不包含选项卡。 you can also work on the tabs with some code like this 您也可以使用以下代码在标签上进行操作

ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(0));

this will allow you to work on Tab 1 and 这将使您能够处理选项卡1和

driver.switchTo().window(tabs2.get(1));

will allow you to work on the second tab.By this way you can handle tab in Browser. 将允许您在第二个选项卡上工作。通过这种方式,您可以处理浏览器中的选项卡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM