Python selenium closes and toggles the Browser tab page

Source: Internet
Author: User

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])


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.