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

后端 未结 4 1724
日久生厌
日久生厌 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 17:42

    An alternative way to open a new window is to use JavaScript and the window handler to switch between them.

    driver = webdriver.Chrome()
    
    # Open a new window
    # This does not change focus to the new window for the driver.
    driver.execute_script("window.open('');")
    
    # Switch to the new window
    driver.switch_to.window(driver.window_handles[1])
    driver.get("http://stackoverflow.com")
    
    # close the active tab
    driver.close()
    
    # Switch back to the first tab
    driver.switch_to.window(driver.window_handles[0])
    driver.get("http://google.se")
    
    # Close the only tab, will also close the browser.
    driver.close()
    

    If you look at your browser while you're executing it will look like the new window has focus, but to the webdriver, it doesn't. Don't be fooled by the visual. Also remember to select a new window handler when you close a tab as it will set the driver.current_window_handle to

    selenium.common.exceptions.NoSuchWindowException: 
        Message: no such window: target window already closed from unknown error: web view not found
      (Session info: chrome=<Your version of chrome>)
      (Driver info: chromedriver=<Your chrome driver version> (<string of numbers>),platform=<Your OS>)
    

    on .close() and it will throw that error if you try to do stuff with the driver at that stage.

    0 讨论(0)
  • 2020-11-27 18:00

    you need to maximize your chrome for this

    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get("https://www.google.com/")
    
    e = driver.find_element_by_tag_name("body")
    ActionChains(driver).key_down(Keys.CONTROL).click(e).send_keys("k").key_up(Keys.CONTROL).perform()
    

    here key_down(Keys.CONTROL) will hold down ctrl key, to get focus on page i am clicking body of the page, then click k

    0 讨论(0)
  • 2020-11-27 18:03

    Here is a simple way, platform independent:

    Code:

    driver.execute_script("window.open('http://google.com', 'new_window')")
    

    Switching back to the original tab:

    Code:

    driver.switch_to_window(driver.window_handles[0])
    

    Checking the current title to be sure you are on the right page:

    Code:

    driver.title
    

    For everything else, have fun!

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题