[Automated Test] Skip login verification code through Cookie

Several methods of processing verification codes are introduced. The last one is to jump to the verification code through cookies, but the details are not enough. Today, I will introduce this method in detail.

[Station B is the easiest to understand] Python interface automation testing from entry to proficiency, ultra-detailed advanced tutorials, just watch this video

Preparation tools:

------------------

fiddler

Python+selenium

------------------

Take Baidu login as an example.

The verification code is in Chinese characters. I think it is a bit difficult and troublesome to identify it through the program.

Next, let’s get started.

1. Open the Fiddler tool, like this!

2. Log in to your Baidu account through the browser. like this!

3. Obtain the cookie of the login request through Fiddler. Find the URL with the Host "passport.baidu.com" and view the cookie of the request in the right window.

Then, find the two important parameters "BAIDUID" and "BDUSS". 4. Write Selenium automated test scripts and skip login.


from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.baidu.com")

# 添加Cookie
driver.add_cookie({'name':'BAIDUID','value':'AAAAAAAAAAAAAA:FG=1'})
driver.add_cookie({'name':'BDUSS','value':'AAAAAAAAAAAAAAAAAAAAAAAAAA'})

# 刷新页面
driver.refresh()

# 获取登录用户名并打印
username = driver.find_element_by_class_name("user-name").text
print(username)

#关闭浏览器
driver.quit()

First, visit the Baidu homepage and be in a non-logged-in state.

Then, add cookie information through the add_cookie() method provided by Selenium.

Finally, refresh the page. Now you are logged in. Get the user name after login and print it.

Guess you like

Origin blog.csdn.net/dad22211/article/details/131773978