Python realizes automatic web page operation

Programming language: python
Integrated development environment (IDE): Visual Studio Code
configuration method refers to Visual Studio Code configuration Python programming environment

1 ready

Recommended to use Chrome browser

1.1 Install selenium package

Activate the virtual environment, open a new Terminal, and enter the following code:

python -m pip install selenium

As shown in the figure below, it means that the installation is successful, the version is 4.7.2,
Successful installation
close the virtual environment, and open a new Terminal

1.2 Import function module

#引入time模块,实现延时
import time
#引入selenium库中的webdriver模块,实现对网页的操作
from selenium import webdriver
#引入By Class,辅助元素定位
from selenium.webdriver.common.by import By
#引入ActionChains Class,辅助鼠标移动
from selenium.webdriver.common.action_chains import ActionChains

2 achieve

2.1 Open the web page

#打开谷歌浏览器
driver = webdriver.Chrome() 
#打开网页
driver.get('URL') #将URL替换为需要操作的网址

2.2 Element positioning

In order to realize the control of the webpage, it is necessary to locate the elements on the webpage first, generally search boxes, options, buttons, etc.

Versions above selenium 4.0 have updated the element positioning method, and the find_element function is used. Examples of usage are as follows:

el = driver.find_element(By.NAME, "*")

First of all, you need to obtain the information of the element:
after opening the web page, press F12to open the element inspection window
Element Inspect Window
. Click the icon marked in the red box in the window to select the element to be located on the page and view its code.
Generally, the element codes that need to be located inputstart with . The following figure shows the code of the search box. From the code, the information needed to locate the element can be extracted: the element can be
search bar
located according to the following information, but it must be ensured that the element can be uniquely located based on the information :

By. *
NAME name=“*”
CLASS_NAME class_name=“*”
ID id=“*”
TAG_NAME tag_name=“*”
LINK_TEXT
PARTIAL_LINK_TEXT
CSS_SELECTOR
XPATH Copy full XPath

If the element cannot be uniquely located based on other information, you can use XPath to locate it. XPath is acquired in a special way. You need to right-click the line of code and select Copy full XPath from the drop-down list, as shown in the following figure:
Copy full XPath

2.3 Element Control

After locating the element, you can perform the following operations to control the element:

method Function
click() click
send_keys(‘*’) enter
clear() empty

Take the input text as an example, the code is as follows (* is the text to be input):

el.send_keys('*')

If the element to be controlled is not within the visible range when opening the web page, and you need to slide the mouse to locate it, you need to use ActionChains, otherwise an error may be reported: the element cannot be found.

ActionChains(driver).move_to_element(el).click().perform()
el.click()

The above example is a click operation. It is best to add the second line to ensure that the element is clicked.
Another reason why the element cannot be found may be that the webpage has not been loaded yet. At this time, the delay function can be used:

time.sleep(1)

2.4 Processing of pop-up boxes

If after clicking the button, instead of opening a new page, a window pops up, and you need to operate in the new window, you need to go to the new frame through the following code, otherwise an error may be reported: the element cannot be found.

driver.switch_to.frame(0)

Then continue to press 2.2 to locate the element.

Guess you like

Origin blog.csdn.net/weixin_50249477/article/details/128714797