Open and close new tab with Selenium WebDriver in OS X

前端 未结 5 532
北恋
北恋 2021-01-11 16:23

I\'m using the Firefox Webdriver in Python 2.7 on Windows to simulate opening (Ctrl+t) and closing (Ctrl + w) a new tab.

相关标签:
5条回答
  • 2021-01-11 16:37

    open a new tab:

    browser.get('http://www.google.com')
    

    close a tab:

    browser.close()
    

    switch to a tab:

    browser.swith_to_window(window_name)
    
    0 讨论(0)
  • 2021-01-11 16:40

    There's nothing easier and clearer than just running JavaScript.

    Open new tab: driver.execute_script("window.open('');")

    0 讨论(0)
  • 2021-01-11 16:47

    Just to combine the answers above for someone still curious. The below is based on Python 2.7 and a driver in Chrome.

    Open new tab by: driver.execute_script("window.open("+URL+", '__blank__');") where URL is a string such as "http://www.google.com".

    Close tab by: driver.close() [Note, this also doubles as driver.quit() when you only have 1 tab open].

    Navigate between tabs by: driver.switch_to_window(driver.window_handles[0]) and driver.switch_to_window(driver.window_handles[1]).

    0 讨论(0)
  • 2021-01-11 16:51

    You can choose which window you want to close:

    window_name = browser.window_handles[0]
    

    Switch window:

    browser.switch_to.window(window_name=window_name)
    

    Then close it:

    browser.close()
    
    0 讨论(0)
  • 2021-01-11 16:55

    Open new tab:

    browser.execute_script("window.open('"+your url+"', '_blank')")
    

    Switch to new tab:

    browser.switch_to.window(windows[1])
    
    0 讨论(0)
提交回复
热议问题