selenium + python自动化登录
实现一个小案例:自动化登录python实训在线
直接上代码:
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()#这里使用火狐驱动geckodriver.log
driver.get("https://www.shixun.online/")#get一下网址
#driver.implicitly_wait(10)#全局睡眠时间
print(driver.title)#判断是否正确
#睡眠时间
#sleep(2)
#登录框
#login_ele = driver.find_element_by_css_selector(".T-icon-txt")
#触发点击事件
#ActionChains(driver).click(login_ele).perform()
ActionChains(driver).click(driver.find_element_by_id("username")).perform()
#查找输入框,输入账号密码,输入框需要提前清理
#设置时间,给浏览器缓冲,不然报错Unable to locate element: #fm-login-id
driver.find_element_by_id("username").clear()#清空
driver.find_element_by_id("username").send_keys("187#######")#账号
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys("187########")#密码
#查找登录按钮
#sleep(2)
driver.find_element_by_css_selector(".btn").click()
#弹窗处理方法
sleep(10)
driver.find_element_by_css_selector("button.btn:nth-child(1)").click()
#sleep(3)
#触发hover事件
ActionChains(driver).click(driver.find_element_by_css_selector(".position-prompt-btn > span:nth-child(2)")).perform()
#获取用户名元素.member-name
user_ele = driver.find_element_by_css_selector(".member-name")
print("======测试结果=======")
print(user_ele.text)
name = user_ele.text
if name == u"20006944":
print("login ok")
else:
print("login fall")
#driver.quit()