Open web in new tab Selenium + Python

后端 未结 11 2195
别跟我提以往
别跟我提以往 2020-11-22 01:15

So I am trying to open websites on new tabs inside my WebDriver. I want to do this, because opening a new WebDriver for each website takes about 3.5secs using PhantomJS, I w

相关标签:
11条回答
  • 2020-11-22 01:47

    Opening the new empty tab within same window in chrome browser is not possible up to my knowledge but you can open the new tab with web-link.

    So far I surfed net and I got good working content on this question. Please try to follow the steps without missing.

    import selenium.webdriver as webdriver
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Chrome()
    driver.get('https://www.google.com?q=python#q=python')
    first_link = driver.find_element_by_class_name('l')
    
    # Use: Keys.CONTROL + Keys.SHIFT + Keys.RETURN to open tab on top of the stack 
    first_link.send_keys(Keys.CONTROL + Keys.RETURN)
    
    # Switch tab to the new tab, which we will assume is the next one on the right
    driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
    
    driver.quit()
    

    I think this is better solution so far.

    Credits: https://gist.github.com/lrhache/7686903

    0 讨论(0)
  • 2020-11-22 01:48

    You can achieve the opening/closing of a tab by the combination of keys COMMAND + T or COMMAND + W (OSX). On other OSs you can use CONTROL + T / CONTROL + W.

    In selenium you can emulate such behavior. You will need to create one webdriver and as many tabs as the tests you need.

    Here it is the code.

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Firefox()
    driver.get("http://www.google.com/")
    
    #open tab
    driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') 
    # You can use (Keys.CONTROL + 't') on other OSs
    
    # Load a page 
    driver.get('http://stackoverflow.com/')
    # Make the tests...
    
    # close the tab
    # (Keys.CONTROL + 'w') on other OSs.
    driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w') 
    
    
    driver.close()
    
    0 讨论(0)
  • 2020-11-22 01:53

    Strangely, so many answers, and all of them are using surrogates like JS and keyboard shortcuts instead of just using a selenium feature:

    def newTab(driver, url="about:blank"):
        wnd = driver.execute(selenium.webdriver.common.action_chains.Command.NEW_WINDOW)
        handle = wnd["value"]["handle"]
        driver.switch_to.window(handle)
        driver.get(url) # changes the handle
        return driver.current_window_handle
    
    0 讨论(0)
  • 2020-11-22 01:55
    • OS: Win 10,
    • Python 3.8.1
      • selenium==3.141.0
    from selenium import webdriver
    import time
    
    driver = webdriver.Firefox(executable_path=r'TO\Your\Path\geckodriver.exe')
    driver.get('https://www.google.com/')
    
    # Open a new window
    driver.execute_script("window.open('');")
    # Switch to the new window
    driver.switch_to.window(driver.window_handles[1])
    driver.get("http://stackoverflow.com")
    time.sleep(3)
    
    # Open a new window
    driver.execute_script("window.open('');")
    # Switch to the new window
    driver.switch_to.window(driver.window_handles[2])
    driver.get("https://www.reddit.com/")
    time.sleep(3)
    # close the active tab
    driver.close()
    time.sleep(3)
    
    # Switch back to the first tab
    driver.switch_to.window(driver.window_handles[0])
    driver.get("https://bing.com")
    time.sleep(3)
    
    # Close the only tab, will also close the browser.
    driver.close()
    

    Reference: Need Help Opening A New Tab in Selenium

    0 讨论(0)
  • 2020-11-22 02:01

    This is a common code adapted from another examples:

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Firefox()
    driver.get("http://www.google.com/")
    
    #open tab
    # ... take the code from the options below
    
    # Load a page 
    driver.get('http://bings.com')
    # Make the tests...
    
    # close the tab
    driver.quit()
    

    the possible ways were:

    1. Sending <CTRL> + <T> to one element

      #open tab
      driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
      
    2. Sending <CTRL> + <T> via Action chains

      ActionChains(driver).key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()
      
    3. Execute a javascript snippet

      driver.execute_script('''window.open("http://bings.com","_blank");''')
      

      In order to achieve this you need to ensure that the preferences browser.link.open_newwindow and browser.link.open_newwindow.restriction are properly set. The default values in the last versions are ok, otherwise you supposedly need:

      fp = webdriver.FirefoxProfile()
      fp.set_preference("browser.link.open_newwindow", 3)
      fp.set_preference("browser.link.open_newwindow.restriction", 2)
      
      driver = webdriver.Firefox(browser_profile=fp)
      

      the problem is that those preferences preset to other values and are frozen at least selenium 3.4.0. When you use the profile to set them with the java binding there comes an exception and with the python binding the new values are ignored.

      In Java there is a way to set those preferences without specifying a profile object when talking to geckodriver, but it seem to be not implemented yet in the python binding:

      FirefoxOptions options = new FirefoxOptions().setProfile(fp);
      options.addPreference("browser.link.open_newwindow", 3);
      options.addPreference("browser.link.open_newwindow.restriction", 2);
      FirefoxDriver driver = new FirefoxDriver(options);
      

    The third option did stop working for python in selenium 3.4.0.

    The first two options also did seem to stop working in selenium 3.4.0. They do depend on sending CTRL key event to an element. At first glance it seem that is a problem of the CTRL key, but it is failing because of the new multiprocess feature of Firefox. It might be that this new architecture impose new ways of doing that, or maybe is a temporary implementation problem. Anyway we can disable it via:

    fp = webdriver.FirefoxProfile()
    fp.set_preference("browser.tabs.remote.autostart", False)
    fp.set_preference("browser.tabs.remote.autostart.1", False)
    fp.set_preference("browser.tabs.remote.autostart.2", False)
    
    driver = webdriver.Firefox(browser_profile=fp)
    

    ... and then you can use successfully the first way.

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