python - 在 Selenium 中控制 Firefox 选项卡

标签 python firefox selenium selenium-webdriver

根据window_handles documentation :

window_handles

Returns the handles of all windows within the current session.

但是,打开新选项卡后,我看不到 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}']

如您所见,window_handles 具有相同的值,但我看到浏览器中打开了 2 个选项卡。难道是我做错了什么?如果是,我该如何获取新标签页的句柄?

使用:

  • Selenium 2.44.0(最新)
  • Firefox 35.0(最新)
  • python 2.7.6
<小时/>

请注意,如果我在 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']

最佳答案

只有顶级浏览器窗口有 HWND。选项卡没有自己的 HWND。如需更多说明,请参阅 here .

关于python - 在 Selenium 中控制 Firefox 选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28079411/

相关文章:

python - Trac 默认语言

html - 除非光标在它们上方,否则 Firefox 中的 blob 图像质量很差

python - Selenium 超时异常: Message: python

python - 如何在 django admin TabularInline 中隐藏删除项目

python - For循环枚举。 Python

python - PyBluez 还活着吗?

html - 为什么在Firefox中默认禁用 "dialog"标签?

html - 如何使用显示: table to get the same row heights in firefox as in chrome and edge

Selenium IDE 命令和assertTextPresent 不起作用

javascript - 如何使用 Selenium 自动点击 = "handleSubmit($event)"