0

I've been digging around this but no luck. So my question is: is not possible to open a new url in a new tab ? ( in this case using IE). My scenario is, open IE, open website, log into it, do some tasks, then open a new tab with a new url in that tab. When I do that, the new url always open in the first tab, not in the new one. I looked for some solutions online but any worked and found many people with same issue. Code follows below:

        from selenium import webdriver
        from selenium.webdriver.common.keys import Keys

        path_to_Ie = 'C:\\Python34\\IEDriver\\IEDriverServer.exe' # change path as needed
        browser = webdriver.Ie(executable_path = path_to_Ie)
        url = 'www.test1.com'
        browser.get(url)


        browser.find_element_by_xpath("//*[@id='username']").send_keys("user")
        browser.find_element_by_xpath("//*[@id='password']").send_keys("pass")

        browser.find_element_by_xpath("//*[@id='login-link']").click()

    # some coding here


        browser.find_element_by_tag_name("body").send_keys(Keys.CONTROL + 't') #open new tab

        browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB) # it looks like it switches to the correct tab, but anyway the browser.get will open the url in the first tab

        browser.get('www.test2.com') # this link must open in this new tab -> but it doesnt.


        import os
        os.system("taskkill /im IEDriverServer.exe")

thank you!

Gonzalo
  • 1,084
  • 4
  • 20
  • 40
  • 1
    Possible duplicate http://stackoverflow.com/questions/17547473/how-to-open-a-new-tab-using-selenium-webdriver – ᴀʀᴍᴀɴ Feb 15 '16 at 08:34
  • @Arman thank you for your comments. already saw before that post. I open the new tab correctly, the issue is that the new URL doesnt open in that tab. Open in the first one,- – Gonzalo Feb 15 '16 at 08:36
  • @Gonzalo Look at Arman's answer, you are not doing it like in that answer. – Fredrik Feb 15 '16 at 08:39
  • @Fredrik I believe it doesnt work also, I've tried also and gives an error: selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN) AttributeError: type object 'Keys' has no attribute 'chord' – Gonzalo Feb 15 '16 at 08:45
  • I guess you should not just open new tab but switch to it also – Andersson Feb 15 '16 at 08:53
  • look at this solution http://stackoverflow.com/questions/28715942/how-do-i-switch-to-the-active-tab-in-selenium – Andersson Feb 15 '16 at 08:55
  • @Andersson It makes sense, though I tried also that but didnt work.. i've read many other questions concerning this but any attempt of solution worked for me.. :/ – Gonzalo Feb 15 '16 at 09:10
  • Quick update, it seems that the the tab switch using for example "browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)" to the second tab, but anyway when i do browser get it comes back to first tab, is that normal? – Gonzalo Feb 15 '16 at 09:46

1 Answers1

0
browser.execute_script("window.open('www.test2.com','_blank');")

This works for Chrome, but in IE it will open new window instead of new tab. If it doesn't matter for you then you can try it

Andersson
  • 51,635
  • 17
  • 77
  • 129