Open web in new tab Selenium + Python

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

提交回复
热议问题