环境搭建:
1.需要配置WebTours网站
2.安装pymysql
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pymysql
3.mysql数据表user表内容
实现代码
# 导入包
from selenium import webdriver
from selenium.webdriver.support.select import Select
from time import sleep
import pymysql
conn = pymysql.connect(host='192.168.66.138',port=3306,user='root',passwd='123456',db='test')
cur = conn.cursor()
sql = 'select * from users where yhm>"zsan5"'
cur.execute(sql)
data = cur.fetchall()
# 打开浏览器
driver = webdriver.Chrome()
dt_data= []
for row in data:
yhm = row[1]
print(yhm)
mm=row[2]
print(mm)
yq=row[3]
print(yq)
# 打开网址
driver.get("http://127.0.0.1:1080/WebTours/")
# 等待3秒
sleep(3)
# 关闭浏览器
# driver.close()
#关闭浏览器 及驱动
# 切换框架
driver.switch_to_default_content()
driver.switch_to_frame("body")
driver.switch_to_frame("info")
driver.find_element_by_link_text("sign up now").click()
sleep(3)
# 跳转页面,还是需要切换框架
driver.switch_to_default_content()
driver.switch_to_frame("body")
driver.switch_to_frame("info")
driver.find_element_by_name("username").send_keys(yhm)
driver.find_element_by_name("password").send_keys(mm)
driver.find_element_by_name("passwordConfirm").send_keys(mm)
driver.find_element_by_name("register").click()
sleep(3)
# 跳转页面,还是需要切换框架
driver.switch_to_default_content()
driver.switch_to_frame("body")
driver.switch_to_frame("info")
expect = yq
actual = driver.find_element_by_tag_name("body").text
# 精确匹配用in 否则用in
if expect in actual:
rs ='pass'
else:
rs='fail'
dt_data.append((rs,yhm))
print(dt_data)
# 批量更新数据
sql = 'update users set rs=%s where yhm=%s'
cur.executemany(sql,dt_data)
conn.commit()
cur.close()
conn.close()
driver.quit()