SoFunction
Updated on 2024-11-19

Browser common basic operations of python3 + selenium4 automation testing (Basic 3)

1、Open the specified web address

When we use selenium for automated testing, the first step after opening the browser is to let the browser access the address we specify, which can be achieved using the get method

from selenium import webdriver
driver = ()
('/')   # This line is used to access the specified address

2、Get the current page url

In the testing process, sometimes we need to get the url of the current page to determine whether to jump to the specified page, get the page url of the following methods:

from selenium import webdriver
driver = ()
('/')
url = driver.current_url    # This line is used to get the url of the current page, i.e. Baidu home page address
print(url)

在这里插入图片描述

3、Back button

The Back button, also known as the ← key in the upper left corner of the browser, is simulated by clicking this button as follows
()

在这里插入图片描述

For example, if you type selenium into the Baidu search box and click Search, then click the Back button, you can realize the following

from selenium import webdriver
from  import By
import time

driver = ()
('/')
driver.find_element(, 'kw').send_keys('selenium')  # Type selenium in the search box
driver.find_element(, 'su').click()     # Click on Baidu
(3)
()  # come (or go) back

This code indicates that after typing selenium in the input box, then click Baidu to search, and then return to Baidu home page after 3 seconds.

4. Forward button

The Forward button, as opposed to the Backward ←, is the → button in the upper-left corner of the browser, and you can operate this button as follows:
()

在这里插入图片描述

For example, type selenium in the Baidu search box and click search, then click the back button and then click the forward button to realize the following

from selenium import webdriver
from  import By
import time

driver = ()
('/')
driver.find_element(, 'kw').send_keys('selenium')  # Type selenium in the search box
driver.find_element(, 'su').click()     # Click on Baidu
()   # Back
(3)
()  # advance

This code indicates that after typing selenium in the input box, and then clicking Baidu to search, and then return to the operation, and then forward to the operation after 3 seconds, and ultimately stays in the search results page after typing selenium.

5. Refresh page

In the testing process, refreshing the page is a frequently used operation, selenium refreshing operation method is as follows
()
Using this method is similar to pressing F5 or clicking the Refresh button in the upper left corner

在这里插入图片描述

from selenium import webdriver

driver = ()
('/')
()    # refresh page

This code means to open Baidu home page and refresh the page

6, get the current page title

During the test, you can use selenium to get the title of the current page in the following way:

在这里插入图片描述

Use selenium to get the title of Baidu home page, the example is as follows:

from selenium import webdriver

driver = ()
('/')
title =   # Get the title of the current page
print(title)

After the above code is run, it will output the title of Baidu's home page on the console Baidu, you will know

7、Window size operation

Commonly used window operations are to set the window size, maximize the window, minimize the window, full-screen window

① Setting the window size
driver.set_window_size(1920, 1080)

② Maximize Window
driver.maximize_window()

③ Minimize the window
driver.minimize_window()
Minimizing the window is a new feature in selenium 4, selenium 3 cannot use this method

④ Full-screen window, equivalent to pressing F11 in most browsers
driver.fullscreen_window()

Sample code:

from selenium import webdriver

driver = ()
('/')
driver.set_window_size(1920, 1080)   # Set window size 1920*1080
driver.minimize_window()  # Minimize the window
driver.maximize_window()  # Maximize the window
driver.fullscreen_window() # full-screen window

This code to open the home page of Baidu, the first browser window size is set to 1920 * 1080, and then in turn minimize the window, maximize the window, full-screen window, the actual test, you need to set up according to needs

8. Withdrawal

Test execution is complete, you need to exit the browser, otherwise multiple runs of the test will lead to a large number of residual system driver processes, these processes will consume computer resources, resulting in more and more system lag, so the development of the test is completed after closing the browser is a good habit!

在这里插入图片描述

Exit the browser as follows:

from selenium import webdriver
from  import By

driver = ()
('/')
driver.find_element(, 'kw').send_keys('selenium')  # Type selenium in the search box
driver.find_element(, 'su').click()     # Click on Baidu
()     # Exit Browser

This code means to open the Baidu home page, then enter selenium in the input box and click Baidu, then exit the browser after completing the search.

The above is the browser common basic operation of python3 + selenium4 automation testing (basic 3) of the details, more information about python3 selenium4 automation testing please pay attention to my other related articles!