Browser operation in python selenium

1. Close all tabs in the browser

driver.quit()

2. Close the current tab (open new tab B from tab A, close tab A)

driver.close()

3. Close the current tab (from tab A) Page A opens a new tab B, closes tab B)

You can use the browser's own shortcut to close the open tab.

The shortcut keys of Firefox are:

Ctrl+t to create a new tab

Ctrl+w to close a tab

 Ctrl+Tab /Ctrl+Page_Up Position the next tab of the current tab

 Ctrl+Shift+Tab/Ctrl+Page_Down Position the previous tab of the current tab

 Ctrl+[Numeric keys 1-8] Position the first [1- 8]

 Ctrl + number key 9 to locate the last tab

Note: If it is 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

#New tab

ActionChains(browser).key_down(Keys.CONTROL).send_keys("t").key_up(Keys.CONTROL).perform()

# close tab

ActionChains(browser).key_down(Keys.CONTROL).send_keys("w").key_up(Keys.CONTROL).perform()


4. Tab switching
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

# traverse the window

for newhandle in handles:

    # Filter newly opened window B

    if newhandle!=handle:

# switch to the newly opened window B

browser.switch_to_window(newhandle)

# Operate in the newly opened window B

browser.find_element_by_id('xx').click()

# close the current window B

browser.close()

#Switch back to window A

browser.switch_to_window(handles[0])




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324712962&siteId=291194637