学习测试10-3自动化 web自动化

web自动化

chrome驱动下载地址:

html 复制代码
https://registry.npmmirror.com/binary.html?path=chromedriver/
https://googlechromelabs.github.io/chrome-for-testing/#stable
观察Google版本,下相应的驱动


运行代码试试,成功Google就会弹出
python 复制代码
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/")
网页有显示监听,可以进行伪装
python 复制代码
# 伪装
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)

准备一个小网页练习

html 复制代码
<!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>

定位元素

python 复制代码
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定位元素

python 复制代码
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

python 复制代码
str1 = driver.find_elements(By.XPATH,'//select/option') # 下拉列表多个option
str1[2].click()   # 再选择元素操作

切窗口

python 复制代码
sleep(2)   # 等待一下
driver.window_handles # 获取所有窗口句柄   每一个窗口对应一个句柄
sleep(2)
winh = driver.current_window_handle # 获取当前窗口句柄  
# print(winh)
driver.switch_to.window(winh[1])    # 切换到指定句柄窗口 索引的方式切
练习
python 复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
driver = webdriver.Chrome()
driver.get("https://www.jd.com/")

driver.find_element(By.XPATH,'//*[@id="navitems-group1"]/li[1]/a').click()
sleep(2)
driver.current_window_handle
win = driver.window_handles
print(win)
driver.switch_to.window(win[1])

driver.find_element(By.XPATH,'//*[@id="nav-fashion"]/a').click()

sleep(2)
driver.current_window_handle
win1 = driver.window_handles
print(win1)
driver.switch_to.window(win1[2])

driver.find_element(By.XPATH,'//*[@id="key"]').send_keys('华为')

============综合练习

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、保存

python 复制代码
# 论坛系统课堂及其课后练习题库
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()

-----------------

python 复制代码
# 二、设置用户的真实姓名   # 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()

----------------

python 复制代码
# 三、单个注册与连续注册多个  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()

-----------------------------

python 复制代码
'''
# 四、完善个人信息  # 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()
相关推荐
一尘之中2 分钟前
网 络 安 全
网络·人工智能·学习·安全
TNT_JQR2 分钟前
电子信息类专业技术学习及比赛路线总结(大一到大三)
单片机·嵌入式硬件·学习
柏箱2 分钟前
PHP基本语法总结
开发语言·前端·html·php
漏刻有时8 分钟前
微信小程序学习实录9:掌握wx.chooseMedia实现多图片文件上传功能(选择图片、预览图片、上传图片)
学习·微信小程序·notepad++
新缸中之脑12 分钟前
Llama 3.2 安卓手机安装教程
前端·人工智能·算法
hmz85616 分钟前
最新网课搜题答案查询小程序源码/题库多接口微信小程序源码+自带流量主
前端·微信小程序·小程序
看到请催我学习22 分钟前
内存缓存和硬盘缓存
开发语言·前端·javascript·vue.js·缓存·ecmascript
blaizeer1 小时前
深入理解 CSS 浮动(Float):详尽指南
前端·css
大霞上仙1 小时前
jmeter学习(7)beanshell
学习·jmeter
大霞上仙1 小时前
jmeter学习(1)线程组与发送请求
java·学习·jmeter