简体   繁体   中英

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

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

Selenium have a Handle for Windows not for tabs. 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

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

will allow you to work on the second tab.By this way you can handle tab in Browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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