Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

python - Controlling firefox tabs in selenium

According to the window_handles documentation:

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:

>>> 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. 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)
  • firefox 35.0 (latest)
  • python 2.7.6

Note that if I would make a similar thing in Chrome, window_handles would show 2 handles:

>>> 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']
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Only the top level browser window has an HWND. Tabs don't have their own HWNDs. For more clarification refer here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...