Selenium won't open a new URL in a new tab (Python & Chrome)

后端 未结 4 1747
日久生厌
日久生厌 2020-11-27 17:03

I want to open quite a few URLs in different tabs using Selenium WebDriver & Python.

I am not sure what is going wrong:

driver = webdriver.Chrome         


        
4条回答
  •  时光说笑
    2020-11-27 18:05

    There is a bug in ChromeDriver that prevents ctrl/command+T from working:

    • I can´t open new tab in ChromeDriver

    What you can do, as a workaround, is to open a link in a new tab and then switch to a new window using the switch_to.window(). Working sample:

    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Chrome()
    driver.get("https://www.google.com")
    
    # open a link in a new window
    actions = ActionChains(driver)
    about = driver.find_element_by_link_text('About')
    actions.key_down(Keys.CONTROL).click(about).key_up(Keys.CONTROL).perform()
    
    driver.switch_to.window(driver.window_handles[-1])
    driver.get("https://stackoverflow.com")
    

    Now the last driver.get() would be performed in a newly opened tab.

提交回复
热议问题