【问题标题】:Selenium webdriver not opening new tab in python [duplicate]Selenium webdriver没有在python中打开新标签[重复]
【发布时间】:2018-03-15 06:15:47
【问题描述】:

下面的代码只打开第一个网站,不打开新标签。如果我让它转到new_tab 之后的新网址,它只会将我发送到同一标签中的不同网址。

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



browser=(r"D:\Developer\Software\Python\chromedriver.exe")
b=webdriver.Chrome(browser)

google=b.get('http://www.google.com')
time.sleep(7)

new_tab=b.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')

【问题讨论】:

    标签: python-3.x selenium selenium-webdriver webdriver


    【解决方案1】:

    试试这个:

    b.execute_script("window.open('http://newsite.com')")
    

    在新标签页中打开网址

    另请注意,您必须切换到新标签才能处理它:

    current = b.current_window_handle
    b.execute_script("window.open('http://newsite.com')")
    new_tab = [tab for tab in b.window_handles if tab != current][0]
    b.switch_to.window(new_tab)
    

    返回主窗口:

    b.switch_to.window(current)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-19
      • 2021-04-27
      • 1970-01-01
      • 2014-03-13
      • 1970-01-01
      • 2015-04-10
      • 1970-01-01
      相关资源
      最近更新 更多