简体   繁体   中英

Python and selenium, is not possible to open a new tab with a URL?

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!

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

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