Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

Multitab issue - Webdriver with Python - Solution provided #5572

Closed
lukeis opened this issue Mar 4, 2016 · 4 comments
Closed

Multitab issue - Webdriver with Python - Solution provided #5572

lukeis opened this issue Mar 4, 2016 · 4 comments

Comments

@lukeis
Copy link
Member

lukeis commented Mar 4, 2016

Originally reported on Google Code with ID 5572

It took me quite a spin to figure this one out.

Issue:
Opening a link in a new tab issue (Multitab) - webdriver with Python

What steps will reproduce the problem?
1.elem = browser.find_element_by_xpath("/html/body/div[2]/div[4]/div/a")
2.elem.send_keys(Keys.CONTROL + Keys.RETURN + "2")
3.for handle in browser.window_handles:
            print "Handle is:" + str(handle)

What is the expected output? What do you see instead?
Expect: Either multiple handles or being able to work with the current focused tab
due to using (Keys.CONTROL + "2")

Actual Output: one handle entry only. any code after that won't work on the tab

Selenium version: 2.32.0
OS:Windows 7
Browser:Firefox
Browser version:20.0.1


Solution:
It seems for some reason although the handle entry is still within the webdriver but
the window has lost sync some how. The solution is to switch the window after opening
the new tab. Here is the code with the fix in it


elem = browser.find_element_by_xpath("/html/body/div[2]/div[4]/div/a") #href link
time.sleep(2) 
elem.send_keys(Keys.CONTROL + Keys.RETURN + "2") #Will open a second tab

#solution for the 2nd issue is here    
for handle in browser.window_handles:
        print "Handle is:" + str(handle) #only one handle number
        browser.switch_to_window(handle)

time.sleep(3)

#Switch the frame over. Even if you have switched it before you need to do it again
browser.switch_to_frame("Frame") 


"""now this is how you handle closing the tab and working again with the original tab"""

#again switch window over
elem.send_keys(Keys.CONTROL + "w")
for handle in browser.window_handles:
    print "HandleAgain is:" + str(handle) #same handle number as before
    browser.switch_to_window(handle)

#again switch frame over if you are working with one
browser.switch_to_frame("Frame") 

time.sleep(3)
#doing a second round/tab
elem = browser.find_element_by_xpath("/html/body/div[2]/div[4]/div/a") #href link
time.sleep(2) 
elem.send_keys(Keys.CONTROL + Keys.RETURN + "2") #open a 2nd tab again

"""Got it? find the handle, switch over the window then switch the frame"""

Reported by logicallayer on 2013-04-30 00:00:46

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Selenium does not support tabs. It is recommended to open links in new windows if you
need to work with several pages at the same time.

Do you really need to open a link in a new tab instead of a new window? Does this affect
the behaviour of the application under test?

Reported by barancev on 2013-04-30 06:55:31

  • Status changed: NeedsClarification

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Tabs would be great to support, also there is so much misleading material on the Internet
that says that tabs are part of window handling. It took me quite some time to find
out why only one window shows in the windows list when I have multiple tabs open.

Reported by jam.may on 2013-08-05 19:31:33

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Tab support does not add new value, instead it adds extra complexity, so it's not going
to be implemented.

Reported by barancev on 2013-10-19 21:50:50

  • Status changed: NotFeasible
  • Labels removed: Status-Untriaged

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Reported by luke.semerau on 2015-09-17 18:17:13

  • Labels added: Restrict-AddIssueComment-Commit

@lukeis lukeis closed this as completed Mar 4, 2016
@SeleniumHQ SeleniumHQ locked and limited conversation to collaborators Mar 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant