Open web in new tab Selenium + Python

后端 未结 11 2194
别跟我提以往
别跟我提以往 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:37

    After struggling for so long the below method worked for me:

    driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
    driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
    
    windows = driver.window_handles
    
    time.sleep(3)
    driver.switch_to.window(windows[1])
    
    0 讨论(0)
  • 2020-11-22 01:40

    I tried for a very long time to duplicate tabs in Chrome running using action_keys and send_keys on body. The only thing that worked for me was an answer here. This is what my duplicate tabs def ended up looking like, probably not the best but it works fine for me.

    def duplicate_tabs(number, chromewebdriver):
    #Once on the page we want to open a bunch of tabs
    url = chromewebdriver.current_url
    for i in range(number):
        print('opened tab: '+str(i))
        chromewebdriver.execute_script("window.open('"+url+"', 'new_window"+str(i)+"')")
    

    It basically runs some java from inside of python, it's incredibly useful. Hope this helps somebody.

    Note: I am using Ubuntu, it shouldn't make a difference but if it doesn't work for you this could be the reason.

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

    In a discussion, Simon clearly mentioned that:

    While the datatype used for storing the list of handles may be ordered by insertion, the order in which the WebDriver implementation iterates over the window handles to insert them has no requirement to be stable. The ordering is arbitrary.


    Using Selenium v3.x opening a website in a New Tab through Python is much easier now. We have to induce an WebDriverWait for number_of_windows_to_be(2) and then collect the window handles every time we open a new tab/window and finally iterate through the window handles and switchTo().window(newly_opened) as required. Here is a solution where you can open http://www.google.co.in in the initial TAB and https://www.yahoo.com in the adjacent TAB:

    • Code Block:

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      
      options = webdriver.ChromeOptions() 
      options.add_argument("start-maximized")
      options.add_argument('disable-infobars')
      driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      driver.get("http://www.google.co.in")
      print("Initial Page Title is : %s" %driver.title)
      windows_before  = driver.current_window_handle
      print("First Window Handle is : %s" %windows_before)
      driver.execute_script("window.open('https://www.yahoo.com')")
      WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
      windows_after = driver.window_handles
      new_window = [x for x in windows_after if x != windows_before][0]
      driver.switch_to_window(new_window)
      print("Page Title after Tab Switching is : %s" %driver.title)
      print("Second Window Handle is : %s" %new_window)
      
    • Console Output:

      Initial Page Title is : Google
      First Window Handle is : CDwindow-B2B3DE3A222B3DA5237840FA574AF780
      Page Title after Tab Switching is : Yahoo
      Second Window Handle is : CDwindow-D7DA7666A0008ED91991C623105A2EC4
      
    • Browser Snapshot:

    multiple__tabs


    Outro

    You can find the java based discussion in Best way to keep track and iterate through tabs and windows using WindowHandles using Selenium

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

    The other solutions do not work for chrome driver v83.

    Instead, it works as follows, suppose there is only 1 opening tab:

    driver.execute_script("window.open('');")
    driver.switch_to.window(driver.window_handles[1])
    driver.get("https://www.example.com")
    

    If there are already more than 1 opening tabs, you should first get the index of the last newly-created tab and switch to the tab before calling the url (Credit to tylerl) :

    driver.execute_script("window.open('');")
    driver.switch_to.window(len(driver.window_handles)-1)
    driver.get("https://www.example.com")
    
    0 讨论(0)
  • 2020-11-22 01:45
    tabs = {}
    
    def new_tab():
        global browser
        hpos = browser.window_handles.index(browser.current_window_handle)
        browser.execute_script("window.open('');")
        browser.switch_to.window(browser.window_handles[hpos + 1])
        return(browser.current_window_handle)
        
    def switch_tab(name):
        global tabs
        global browser
        if not name in tabs.keys():
            tabs[name] = {'window_handle': new_tab(), 'url': url+name}
            browser.get(tabs[name]['url'])
        else:
            browser.switch_to.window(tabs[name]['window_handle'])
    
    0 讨论(0)
  • 2020-11-22 01:47
    browser.execute_script('''window.open("http://bings.com","_blank");''')
    

    Where browser is the webDriver

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