Open web in new tab Selenium + Python

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

提交回复
热议问题