Python — Opening multiple tabs using Selenium

后端 未结 3 1382
天命终不由人
天命终不由人 2020-12-09 12:44

I am using Python. I am trying to open two tabs on chrome, each to a different website. This is my code:

from selenium import webdriver
from selenium.webdriv         


        
相关标签:
3条回答
  • 2020-12-09 13:19

    You should switch to other tab to interact with it.

    ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
    driver.switchTo().window(tabs.get(0));//first tab
    driver.switchTo().window(tabs.get(1));//second tab
    
    0 讨论(0)
  • 2020-12-09 13:28

    try like this for python:

    browser=webdriver.Chrome()
    browser.get('http:/reddit.com')
    window_before = driver.window_handles[0]
    browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
    window_after = driver.window_handles[1]
    driver.switch_to_window(window_after)
    time.sleep(3)
    browser.get('http://bing.com')
    
    0 讨论(0)
  • 2020-12-09 13:32

    To interact with a window, you need to set the context to that window with driver.switch_to.window. It would also be easier to open a new tab with a script injection:

    browser=webdriver.Chrome()
    
    #first tab
    browser.get('http:/reddit.com')
    
    #second tab
    browser.execute_script("window.open('about:blank', 'tab2');")
    browser.switch_to.window("tab2")
    browser.get('http://bing.com')
    
    0 讨论(0)
提交回复
热议问题