Python使用selenium进行浏览器账户测试
Posted hhh江月
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python使用selenium进行浏览器账户测试相关的知识,希望对你有一定的参考价值。
Python使用selenium进行浏览器账户测试
文章目录
一、简介
这里,我们将使用Python的selenium模块对某个网站的账户进行自动化测试。
https://djs0pkeif39feujmxds.appdjujengbjnsdbbsdbsb20.com/register
二、使用requests进行初步测试
首先使用Python的requests模块进行初步的检测。
import requests
url = "https://djs0pkeif39feujmxds.appdjujengbjnsdbbsdbsb20.com/register"
res = requests.get(url)
print(res.content.decode())
三、使用selenium进行自动化测试
在使用Python的selenium模块进行自动化测试的同时,也对滑块进行了自动的验证测试。
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import random
url = "https://djs0pkeif39feujmxds.appdjujengbjnsdbbsdbsb20.com/register"
num_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
while True:
str000 = ''
for i in range(8):
str000 += str(random.sample(num_list, 1)[0])
driver= webdriver.Chrome()
driver.get(url=url)
time.sleep(3)
driver.find_elements(by=By.CLASS_NAME, value='van-field__control')[0].send_keys(str000)
time.sleep(0.2)
driver.find_elements(by=By.CLASS_NAME, value='van-field__control')[1].send_keys('yxhu' + str000 + 'flgz')
time.sleep(0.2)
driver.find_elements(by=By.CLASS_NAME, value='van-field__control')[2].send_keys('flgz' + str000 + 'yxhu')
time.sleep(0.2)
driver.find_element(by=By.XPATH, value='//*[@id="app"]/div[1]/div/div/div/div[2]/button').click()
time.sleep(1.4)
try:
driver.find_element(by=By.XPATH, value='/html/body/div[3]/div[2]/button[2]').click()
except Exception as e:
print(e)
else:
print('success')
finally:
print('finally')
time.sleep(1)
slider = driver.find_element(by=By.XPATH, value='//*[@id="nc_2_n1z"]')
# 解决特征识别的代码
script = 'Object.defineProperty(navigator, "webdriver", get: () => false,);'
driver.execute_script(script)
# 滑块解锁
action = ActionChains(driver)
action.click_and_hold(slider) # 按住
action.move_by_offset(954, 0) # 往右偏移248个像素
action.release() # 释放鼠标
action.perform() # 执行
time.sleep(10)
try:
domwords = driver.find_element(by=By.XPATH, value='/html/body/div[3]/div[1]')
except Exception as e:
print(e)
else:
print(domwords.text)
if domwords.text == "邀请码错误或者已过期":
driver.quit()
continue
driver.find_element(by=By.XPATH, value='/html/body/div[3]/div[2]/button[2]').click()
time.sleep(10)
driver.find_element(by=By.XPATH, value='//*[@id="app"]/div[1]/div/div/div/div[6]/div[1]/div[3]/button[1]').click()
time.sleep(10)
driver.quit()
四、效果展示
请参见哔哩哔哩视频:
https://www.bilibili.com/video/BV1P44y1P7mc?spm_id_from=333.999.0.0
五、总结
在进行测试的时候,主要是使用了路径的锁定以及滑块的处理,经过这次实践,我对于selenium的理解更深一层了,学些到了不少东西,值得记录一下。
下面整理一下重要的代码:
while True:
str000 = ''
for i in range(8):
str000 += str(random.sample(num_list, 1)[0])
driver= webdriver.Chrome()
driver.get(url=url)
time.sleep(3)
driver.find_elements(by=By.CLASS_NAME, value='van-field__control')[0].send_keys(str000)
time.sleep(0.2)
driver.find_elements(by=By.CLASS_NAME, value='van-field__control')[1].send_keys('yxhu' + str000 + 'flgz')
time.sleep(0.2)
driver.find_elements(by=By.CLASS_NAME, value='van-field__control')[2].send_keys('flgz' + str000 + 'yxhu')
time.sleep(0.2)
driver.find_element(by=By.XPATH, value='//*[@id="app"]/div[1]/div/div/div/div[2]/button').click()
time.sleep(1.4)
try:
driver.find_element(by=By.XPATH, value='/html/body/div[3]/div[2]/button[2]').click()
except Exception as e:
print(e)
else:
print('success')
finally:
print('finally')
time.sleep(1)
slider = driver.find_element(by=By.XPATH, value='//*[@id="nc_2_n1z"]')
# 解决特征识别的代码
script = 'Object.defineProperty(navigator, "webdriver", get: () => false,);'
driver.execute_script(script)
# 滑块解锁
action = ActionChains(driver)
action.click_and_hold(slider) # 按住
action.move_by_offset(954, 0) # 往右偏移248个像素
action.release() # 释放鼠标
action.perform() # 执行
time.sleep(10)
try:
domwords = driver.find_element(by=By.XPATH, value='/html/body/div[3]/div[1]')
except Exception as e:
print(e)
else:
print(domwords.text)
if domwords.text == "邀请码错误或者已过期":
driver.quit()
continue
driver.find_element(by=By.XPATH, value='/html/body/div[3]/div[2]/button[2]').click()
time.sleep(10)
driver.find_element(by=By.XPATH, value='//*[@id="app"]/div[1]/div/div/div/div[6]/div[1]/div[3]/button[1]').click()
time.sleep(10)
driver.quit()
最后,谢谢大家的阅读与支持,期待大家的点赞与评论。
以上是关于Python使用selenium进行浏览器账户测试的主要内容,如果未能解决你的问题,请参考以下文章
Python 使用 selenium 进行自动化测试 或者协助日常工作
[Selenium自动化测试学习]Python+Selenium环境搭建