how to open two tabs in python splinter

2019-05-19 10:22发布

问题:

from splinter import Browser

with Browser() as browser:
      browser.visit('https://google.com/')
      browser.visit('http://www.bing.com/')

      browser.windows[0]
      browser.windows[1]

How to open two tabs in python splinter and change tab?

回答1:

import time

from splinter import Browser
from selenium.webdriver.common.keys import Keys


with Browser() as browser:

    # visit fst url
    browser.visit('https://google.com/')
    # open new tab
    browser.driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
    # visit second url
    browser.visit('https://www.bing.com/')

    # switching between tabs
    browser.driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
    time.sleep(1)

    browser.driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.SHIFT + Keys.TAB)
    time.sleep(1)

    browser.driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)

    # visit third url in fst tab
    browser.visit('https://stackoverflow.com/')