web自动化
chrome驱动下载地址:
https://registry.npmmirror.com/binary.html?path=chromedriver/
https://googlechromelabs.github.io/chrome-for-testing/#stable
观察Google版本,下相应的驱动
运行代码试试,成功Google就会弹出
from selenium import webdriver
from time import sleep # 时间模块 让浏览器等待,便于展示
# Chrome("webdriver驱动绝对路径") 和谷歌浏览器放在一起
driver = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get("https://www.baidu.com/")
网页有显示监听,可以进行伪装
# 伪装
from selenium import webdriver
option = webdriver.ChromeOptions()
Chrome_driver = "C:\Program Files\Google\Chrome\Application\chromedriver.exe"
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(Chrome_driver, options=option)
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
{'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'})
url = "https://www.baidu.com"
driver.get(url)
准备一个小网页练习
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>网页测试</title>
</head>
<body align="center">
<h1>欢迎来到软测!</h1>
<h2>Welcome to ShangHai</h2>
用户名<input type="text" id="input_01">
<input type="checkbox">自动登录<br>
密 码<input type="text" name="input_02">
<input type="radio">记住密码<br>
<input type="button" value="登录" class="sk">
<button type="">注册</button>
<a href="https://v.qq.com/">百度一下</a><br>
登录方式<select>
<option>手机号登录</option>
<option>qq登录</option>
<option>微信登录</option>
</select>
<br><button ondblclick="javascript:alert('警告!!')" id="click">这是双击按钮</button>
</body>
</html>
定位元素
from selenium import webdriver
from time import sleep # 时间模块 让浏览器等待,便于展示
from selenium.webdriver.common.by import By
driver = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get("file:///C:/Users/admin/Desktop/mingying.html")
# 对元素操作 优先使用id,name,XPATH
driver.find_element(By.ID, 'input_01').send_keys('admin') # id
driver.find_element(By.NAME, 'input_02').send_keys('123456') # name
driver.find_element(By.CLASS_NAME, 'sk').click() # class
# driver.find_element(By.TAG_NAME, 'p').click() # tag 通过标签定义元素 <p></p>
# driver.find_element(By.LINK_TEXT, '百度一下').click() # 链接的所有文本,不能少 <a>百度一下</a>
# driver.find_element(By.PARTIAL_LINK_TEXT, '百度').click() # 链接的部分文本 <a>百度一下</a>
driver.find_element(By.XPATH, '/html/body/input[5]').click() # XPATH
driver.find_element(By.NAME, 'input_02').clear() # 清空
# sleep(5) # 等待5秒再操作
# 对网页的操作
# driver.quit() # 退出当前网页
# driver.close() # 关闭浏览器
详解用By.XPATH定位元素
from selenium import webdriver
from time import sleep # 时间模块 让浏览器等待,便于展示
from selenium.webdriver.common.by import By
driver = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get("file:///C:/Users/admin/Desktop/mingying.html")
# 对元素操作 优先使用id,name,XPATH
driver.find_element(By.XPATH, '/html/body/select/option[3]').click()
driver.find_element(By.XPATH, '//select/option[2]').click()
driver.find_element(By.XPATH, '//div[@id="su"]/input[@name="01"]').click() # XPATH 相对路径加标签属性 //input[@属性='值']
# XPATH 模糊定位starts-with:id=input_01 '//标签[starts-with(@属性,值)]'
driver.find_element(By.XPATH, '//input[starts-with(@id,input)]').send_keys('eeeee')
# XPATH 模糊定位contains:id=input_01 //标签[contains(@属性,"值")]
driver.find_element(By.XPATH, '//input[contains(@id,"input")]').send_keys('wwww')
# XPATH 模糊定位contains:text() //标签[contains(text(),"值")]
driver.find_element(By.XPATH, '//a[contains(text(),"百度一下")]').click()
driver.find_element(By.NAME, 'input_02').clear() # 清空
driver.quit() # 退出当前网页
定位一组元素 find_elements
str1 = driver.find_elements(By.XPATH,'//select/option') # 下拉列表多个option
str1[2].click() # 再选择元素操作
切窗口
sleep(2) # 等待一下
driver.window_handles # 获取所有窗口句柄 每一个窗口对应一个句柄
sleep(2)
winh = driver.current_window_handle # 获取当前窗口句柄
# print(winh)
driver.switch_to.window(winh[1]) # 切换到指定句柄窗口 索引的方式切
============综合练习
1 自动化需要的包和测试软件
链接: https://pan.baidu.com/s/1QnGEAeP4ASMgdBYbAtSUTg?pwd=8sdd 提取码: 8sdd
复制这段内容后打开百度网盘手机App,操作更方便哦
安装步骤WAMP
论坛系统课堂及其课后练习题库
一、自动登录
1、点击登录按钮
2、输入用户名,密码,(使用id属性)
3、点击登录
二、设置用户的真实姓名
1、登录
2、点击设置超链接
3、输入用户名
4、点击保存
三、单个注册与连续注册多个
1、点击立即注册
2、输入四个输入框
3、提交
4、实现连续注册10个账号
四、完善个人信息
1、登录
2、点击设置
3、基本资料栏位依次输入内容
性别、生日、出生地、居住地、情感状态、交友目的、血型
4、所有信息后的下拉框设置为保密
5、保存
# 论坛系统课堂及其课后练习题库
from selenium import webdriver
from time import sleep # 时间模块 让浏览器等待,便于展示
from selenium.webdriver.common.by import By
# 一、自动登录
# 1、点击登录按钮 # 2、输入用户名,密码,(使用id属性)# 3、点击登录
driver = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get("http://127.0.0.1/Discuz/upload/forum.php")
driver.find_element(By.ID, 'ls_username').send_keys('admin') # id
driver.find_element(By.ID, 'ls_password').send_keys('123456')
driver.find_element(By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button').click() # 登录
sleep(3)
driver.quit()
-----------------
# 二、设置用户的真实姓名 # 1、登录 3、输入用户名 4、点击保存
from selenium import webdriver
from time import sleep # 时间模块 让浏览器等待,便于展示
from selenium.webdriver.common.by import By
driver = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get("http://127.0.0.1/Discuz/upload/forum.php")
# -------------------
driver.find_element(By.ID, 'ls_username').send_keys('admin') # id
driver.find_element(By.ID, 'ls_password').send_keys('123456')
driver.find_element(By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button').click() # 登录
sleep(1)
driver.find_element(By.XPATH, '//*[@id="um"]/p[1]/strong/a').click() # 点击admin
sleep(1) # 等待一下
driver.current_window_handle # 获取当前窗口句柄
win = driver.window_handles
print(win)
driver.switch_to.window(win[1]) # 切换到指定句柄窗口 索引的方式切
sleep(1) # 等待一下
driver.find_element(By.XPATH, '//*[@id="pcd"]/div/ul[1]/li[4]/a').click() # 点击资料管理
sleep(1)
driver.find_element(By.XPATH, '//*[@id="td_realname"]/input').send_keys('小王')
driver.find_element(By.XPATH, '//*[@id="profilesubmitbtn"]').click() #
sleep(3)
driver.quit()
----------------
# 三、单个注册与连续注册多个 1、点击立即注册 2、输入四个输入框 3、提交 4、实现连续注册10个账号
from selenium import webdriver
from time import sleep # 时间模块 让浏览器等待,便于展示
from selenium.webdriver.common.by import By
driver = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
# -------------------
user = ['aa1121', 'aa1a', 'aa1aa', 's1s1', 'ss1s', 's1sss']
pad = ['123', '12134', '123451', '1234156', '123412', '1213123']
email = ['12111213@qq.com', '12314@qq.com', '123145@qq.com', '1231456@qq.com', '123412@qq.com', '1231123@qq.com']
for i in range(0, 6):
driver.get("http://127.0.0.1/Discuz/upload/forum.php")
driver.find_element(By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[4]/a').click() # 点击注册
driver.find_element(By.XPATH, '//*[@id="FMjM6T"]').send_keys(user[i])
driver.find_element(By.XPATH, '//*[@id="RD3S6a"]').send_keys(pad[i])
driver.find_element(By.XPATH, '//*[@id="2XxNnK"]').send_keys(pad[i])
driver.find_element(By.XPATH, '//*[@id="5uzNiN"]').send_keys(email[i])
driver.find_element(By.XPATH, '//*[@id="registerformsubmit"]').click()
sleep(5)
driver.find_element(By.XPATH, '//*[@id="um"]/p[1]/a[4]').click() # 退出
continue
sleep(3)
driver.quit()
-----------------------------
'''
# 四、完善个人信息 # 1、登录 # 2、点击设置
# 3、基本资料栏位依次输入内容性别、生日、出生地、居住地、情感状态、交友目的、血型 4、所有信息后的下拉框设置为保密 5、保存
from selenium import webdriver
from time import sleep # 时间模块 让浏览器等待,便于展示
from selenium.webdriver.common.by import By
driver = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get("http://127.0.0.1/Discuz/upload/forum.php")
# -------------------
driver.find_element(By.ID, 'ls_username').send_keys('admin') # id
driver.find_element(By.ID, 'ls_password').send_keys('123456') # name
driver.find_element(By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button').click() # 登录
sleep(1)
driver.find_element(By.XPATH, '//*[@id="um"]/p[1]/a[1]').click() #
driver.find_element(By.XPATH, '//*[@id="td_realname"]/input').send_keys('小王2')
driver.find_element(By.XPATH, '//*[@id="gender"]/option[2]').click() #性别
driver.find_element(By.XPATH, '//*[@id="birthyear"]/option[45]').click() #生日
driver.find_element(By.XPATH, '//*[@id="birthmonth"]/option[7]').click()
driver.find_element(By.XPATH, '//*[@id="birthday"]/option[19]').click()
driver.find_element(By.XPATH, '//*[@id="td_birthcity"]/a').click() # 修改
sleep(1)
driver.find_element(By.XPATH, '//*[@id="birthprovince"]/option[@did="17"]').click() #出生地
sleep(1)
driver.find_element(By.XPATH, '//*[@id="birthcity"]/option[@did="267"]').click()
sleep(1)
driver.find_element(By.XPATH, '//*[@id="birthdist"]/option[@did="2890"]').click()
sleep(1)
driver.find_element(By.XPATH, '//*[@id="birthcommunity"]/option[@did="26249"]').click()
sleep(1)
driver.find_element(By.XPATH, '//*[@id="td_residecity"]/a').click() # 修改
sleep(1)
driver.find_element(By.XPATH, '//*[@id="resideprovince"]/option[@did="10"]').click() #居住地
sleep(1)
driver.find_element(By.XPATH, '//*[@id="residecity"]/option[@did="166"]').click()
sleep(1)
driver.find_element(By.XPATH, '//*[@id="residedist"]/option[@did="2072"]').click()
sleep(1)
driver.find_element(By.XPATH, '//*[@id="residecommunity"]/option[@did="14283"]').click()
driver.find_element(By.XPATH, '//*[@id="td_affectivestatus"]/input').send_keys('单') #情感状态
driver.find_element(By.XPATH, '//*[@id="td_lookingfor"]/input').send_keys('一起打游戏') #交友目的
driver.find_element(By.XPATH, '//*[@id="td_bloodtype"]/select/option[3]').click() #血型
# 设置保密
driver.find_element(By.XPATH, '//*[@id="tr_realname"]/td[2]/select/option[3]').click()
driver.find_element(By.XPATH, '//*[@id="tr_gender"]/td[2]/select/option[3]').click()
driver.find_element(By.XPATH, '//*[@id="tr_birthday"]/td[2]/select/option[3]').click()
driver.find_element(By.XPATH, '//*[@id="tr_birthcity"]/td[2]/select/option[3]').click()
driver.find_element(By.XPATH, '//*[@id="tr_residecity"]/td[2]/select/option[3]').click()
driver.find_element(By.XPATH, '//*[@id="tr_affectivestatus"]/td[2]/select/option[3]').click()
driver.find_element(By.XPATH, '//*[@id="tr_lookingfor"]/td[2]/select/option[3]').click()
driver.find_element(By.XPATH, '//*[@id="tr_bloodtype"]/td[2]/select/option[3]').click()
sleep(1)
driver.find_element(By.XPATH, '//*[@id="profilesubmitbtn"]').click() # 保存
sleep(3)
driver.quit()