Selenium automated login (actual analysis)

Table of contents

1. Plain English verification code

1. First, you need to understand the modules required to run

2. Get our target address link

3. Enter the first page of ancient poetry, find the label of the verification code, obtain and save it

4. Call Super Eagle coding platform

5. Call the method in Super Eagle

6. Enter account password

7. The simulated login needs to use cookies to keep the login status


I believe that many novice Xiaobai have encountered the same problem as me, and there have been many problems in the identification of verification codes.
Well, I am a little confused about the method of submitting the verification codes of those screenshots, but I can't understand the cases given by some coding platforms, and I haven't been able to figure it out after a few days of work. This one is very painful. So for automated login, there are many types of verification codes. The examples are as follows:

  • plain english
  • pure numbers
  • English plus numbers
  • slider
  • double slider
  • Calculation
    and so on a series of types. So today I will write my own. I worked on the slider verification code for a few days, and the pattern click verification code, but I didn’t get it out so I gave up and turned to the simpler verification code. Example of plain English verification code. (Take Gushici.com as an example)

1. Plain English verification code

1. First, you need to understand the modules required to run

```
from selenium import webdriver#浏览器驱动
from PIL import Image#ORC识别图片
from pc.chaojiying_Python.chaojiying import Chaojiying_Client
from selenium.webdriver.common.by import By#元素查找方法
from requests_html import HTMLSession
import time
```

2. Get our target address link

```
# 创建请求对象
session = HTMLSession()
# 创建浏览器驱动对象
driver = webdriver.Chrome()

url = 'http://www.jianjiaoshuju.com/path/login.htm'
driver.maximize_window()
driver.get(url)
```

3. Enter the first page of ancient poetry, find the label of the verification code, obtain and save it

```
img = driver.find_element(By.XPATH, '//*[@id="imgCode"]')

img.screenshot('gus.png')
```

4. Call Super Eagle coding platform


After downloading, copy and paste the folder in your own project directory, and import

`from pc.chaojiying_Python.chaojiying import Chaojiying_Client`

5. Call the method in Super Eagle

```
chaojiying = Chaojiying_Client('超级鹰账号', '密码', '软件id号')	#用户中心>>软件ID 生成一个替换 96001
im = open('gus.png', 'rb').read()													#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
result = chaojiying.PostPic(im, 3004)#3004,验证码类型
print(result)
```

6. Enter account password

```
driver.find_element(By.XPATH, '//*[@id="email"]').send_keys('古诗词账号')
time.sleep(0.5)
driver.find_element(By.XPATH, '//*[@id="pwd"]').send_keys('密码')
time.sleep(0.5)
driver.find_element(By.XPATH, '//*[@id="code"]').send_keys(result)

time.sleep(2)
# 点击确定按钮
driver.find_element(By.XPATH, '//*[@id="denglu"]').click()
time.sleep(2)
```

7. The simulated login needs to use cookies to keep the login status

```
"""
selenium自动化登录,获取cookie,结合requests获取登录之后的数据
将cookie列表里面里面的元素进行key取value来组合新的字典
name的value值作为key值
value的value值作为value
"""
cookie_dict = {cookie['name']: cookie['value'] for cookie in driver.get_cookies()}
url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
headers = {
    # 'Host': 'www.jianjiaoshuju.com',
    # 'Referer': 'http://www.jianjiaoshuju.com/path/login.htm',
    # 'Upgrade-Insecure-Requests': '1',
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
}
response = session.get(url, headers=headers, cookies=cookie_dict).content.decode()
print(response)

```

 


Finally, I would like to thank everyone who has read my article carefully. Looking at the fans’ growth and attention all the way, there is always a need for reciprocity. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can help you too!

Guess you like

Origin blog.csdn.net/weixin_67553250/article/details/131440108