Open web in new tab Selenium + Python

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

提交回复
热议问题