简体   繁体   English

Python和Selenium,无法使用URL打开新标签页吗?

[英]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 ? 所以我的问题是:不能在新选项卡中打开新的URL吗? ( in this case using IE). (在这种情况下,使用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. 我的情况是,打开IE,打开网站,登录,执行一些任务,然后在该选项卡中打开带有新URL的新选项卡。 When I do that, the new url always open in the first tab, not in the new one. 当我这样做时,新的URL总是在第一个选项卡中打开,而不是在新的选项卡中打开。 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. 这适用于Chrome ,但在IE中将打开新窗口,而不是新标签页。 If it doesn't matter for you then you can try it 如果对您来说没关系,那么您可以尝试一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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