1. Close Browser All tabs
Driver.quit ()
2. Close the current tab page (open a new tab from Tab A, B, close tab a)
Driver.close ()
3. Close the current tab page (open a new tab from Tab A, B, and close tab B)
Open tabs can be closed with shortcuts from the browser
Firefox's own shortcut keys are:
Ctrl+t new Tab
Ctrl+w Close Tab
CTRL+TAB/CTRL+PAGE_UP Locate the next tab in the current tab page
Ctrl+shift+tab/ctrl+page_down Position the previous tab page of the current tab
ctrl+[number key 1-8] locates the top [1-8] of all tabs
CTRL + Number key 9 to locate the last tab page
Note: If you are in some Linux distribution systems, such as Ubuntu, you need to replace the CTRL key with the ALT key
From selenium import Webdriver
From Selenium.webdriver.common.keys import keys
From Selenium.webdriver.common.action_chains import Actionchains
#新建标签页
Actionchains (browser). Key_down (Keys.control). Send_keys ("T"). KEY_UP (Keys.control). Perform ()
# Close Tag Page
Actionchains (browser). Key_down (Keys.control). Send_keys ("W"). Key_up (Keys.control). Perform ()
4. tab page Toggle
From selenium import Webdriver
Browser=webdriver. Firefox ()
Browser.get (' xxxxx ')
# Get the current Window handle (window a)
Handle = Browser.current_window_handle
# Open a new window
browser.find_element_by_id (' xx '). Click ()
# Get all current window handles (window A, B)
Handles = Browser.window_handles
# to traverse a window
For Newhandle in handles:
# Filter The newly opened window b
If Newhandle!=handle:
# Switch to the newly opened window b
Browser.switch_to_window (Newhandle)
# operating in a newly opened window B
browser.find_element_by_id (' xx '). Click ()
# Close Current Window b
Browser.close ()
#切换回窗口A
Browser.switch_to_window (Handles[0])