环境搭建:
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()
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'
sql = 'update users set rs="'+rs+' " where yhm="'+yhm+'"'
print(sql)
cur.execute(sql)
conn.commit() # 提交到数据库中,不然只放在缓存中
cur.close()
conn.close()
driver.quit()