前提:公司的路由器密码忘记了,试了好多个没对,懒得手动测试,然后想试试python+selenium的网页自动操作功能,参考了某大神的教程,结果如下:
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
url = 'http://192.168.xx.1/webpages/login.html' #配置测试地址
wd = webdriver.Chrome() #调用chrome,这里将chromedriver.exe放入python目录中,不用路径调用
wd.get(url)
f = open(r'e:/passwd1.txt') #配置密码文件路径
for i in f.readlines():
time.sleep(1) #等待页面刷新
input_account = wd.find_element(By.ID,'username')
input_account.send_keys('admin') #使用固定用户admin
i = i.strip('\n')
input_password = wd.find_element(By.XPATH,'//*[@type="password"]')
input_password.send_keys(i)
login_button = wd.find_element(By.ID,'login-btn')
login_button.click()
print(i)
time.sleep(0.1) #等待结果
if(wd.find_element(By.XPATH,'//div//h3')):
# 这里需要一个登陆错误判定,密码错误时,

本文记录了一次使用Python结合Selenium进行路由器登录密码自动化测试的过程。遇到的问题包括:Selenium指令更新、密码输入框ID的正确定位(需通过XPath找到隐藏的password元素),以及解决因页面加载速度导致的元素未找到问题,通过添加延时解决了这一问题。
最低0.47元/天 解锁文章
2116

被折叠的 条评论
为什么被折叠?



