本篇文章为大家展示了selenium自动化测试的示例分析,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
下载驱动器
http://chromedriver.storage.googleapis.com/index.html
下载与谷歌版本相同或最近版本。
chrome版本查看
帮助中查看
导包
from selenium import webdriver
创建浏览器对象(以chrome为例)
driverpath = r' '# 驱动器路径(chromedriver.exe)driver = webdriver.Chrome(executable_path=driverpath)
demo
import timefrom selenium import webdriverurl = 'https://www.baidu.com'driverPath = r'' # 驱动器路径driver = webdriver.Chrome(executable_path=driverPath)driver.get(url)driver.maximize_window() # 最大化页面time.sleep(3)driver.quit()
基本函数
尺寸,位置
driver.minimize_window() #最大化页面driver.minimize_window() #最小化页面driver.set_window_size(200, 100) #设置窗口大小print('窗口尺寸:' + str(driver.get_window_size())) #窗口大小driver.set_window_position(200, 300) # 设置窗口位置
关闭
driver.quit()driver.close()
页面基本操作
driver.get(url) # 获取一个页面driver.back() # 回退driver.forward() # 前进
demo
import timefrom selenium import webdriverurl1 = 'https://www.baidu.com'url2 = 'https://www.bilibili.com'driverPath = r'C:\Users\29273\AppData\Local\Programs\Python\Python38-32\chromedriver.exe'driver = webdriver.Chrome(executable_path=driverPath)driver.set_window_position(200, 300)driver.get(url1)time.sleep(5)driver.get(url2)time.sleep(5)driver.back()time.sleep(5)driver.close()
元素定位以及数据输入
div标签中的属性来进行定位
id,classname,name,tagname,partial link text,link text,css selector
例如下列a标签中的classname为username
driver.find_element_by_class_name("username")
driver.find_element_by_class_name("btn.btn_big.btn_error")
# 部分函数driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[3]/div[2]/div[1]/dl/dd/div[1]/a')driver.find_element_by_css_selector('body > div.wrap > div.main_wrap > div.main.cc > div.main_sidebar > div.box_wrap.user_info > dl > dd > div.name > a')driver.find_element_by_id(value)driver.find_element_by_name(value)driver.find_element_by_class_name(value)driver.find_element_by_tag_name(value)driver.find_element_by_link_text(value)driver.find_element_by_partial_link_text(value)
对数据框输入数据
driver.find_element_by_name("username").send_keys("giao")
点击事件
driver.find_element_by_xpath('//*[@id="J_register_form"]/div/dl[5]/dd/button').click()
练习一(简单网页注册)
http://47.107.178.45/phpwind/
import timefrom selenium import webdriverurl = r'http://47.107.178.45/phpwind/'driverPath = r'C:\Users\29273\AppData\Local\Programs\Python\Python38-32\chromedriver.exe'driver = webdriver.Chrome(executable_path=driverPath)driver.get(url)registerBtn = driver.find_element_by_xpath('//*[@id="J_login_form"]/dl/dd[2]/a')registerBtn.click()time.sleep(5)driver.find_element_by_name("username").send_keys("giao")driver.find_element_by_name("password").send_keys("giaogiao")driver.find_element_by_name("repassword").send_keys('giaogiao')driver.find_element_by_name("email").send_keys('giao@qq.com')driver.find_element_by_xpath('//*[@id="J_register_form"]/div/dl[5]/dd/button').click()time.sleep(10)driver.quit()
练习二
打开某民宿网站->输入城市->选择人数->点击查看详情
import timefrom selenium import webdrivercity = input('请输入你想查询的城市:')adult = input('请输入大人人数:')teen = input('请输入孩子人人数:')baby = input('请输入婴儿人数:')url = "https://www.airbnb.cn/?logo=1"driverPath = r'C:\Users\29273\AppData\Local\Programs\Python\Python38-32\chromedriver.exe'driver = webdriver.Chrome(executable_path=driverPath)driver.get(url)city_input = driver.find_element_by_id("Koan-via-HeaderController__input")city_input.send_keys(city + "\n")time.sleep(5)driver.find_element_by_name("query").click()time.sleep(3)for i in range(int(adult)): driver.find_element_by_xpath( '//*[@id="menuItemComponent-guest_picker"]/div/div/div/div[1]/div/div/div[1]/div/div/div/div/div/div[2]/div/div[3]/button').click()time.sleep(4)for i in range(int(teen)): driver.find_element_by_xpath( '//*[@id="menuItemComponent-guest_picker"]/div/div/div/div[1]/div/div/div[2]/div/div/div/div/div/div[2]/div/div[3]/button').click()time.sleep(3)for i in range(int(baby)): driver.find_element_by_xpath( '//*[@id="menuItemComponent-guest_picker"]/div/div/div/div[1]/div/div/div[3]/div/div/div/div/div/div[2]/div/div[3]/button').click()time.sleep(5)driver.find_element_by_class_name("_fhph5u")[0].click()time.sleep(20)
练习三
爬取某站入站必看视频信息
import timefrom selenium import webdriverurl = 'https://www.baidu.com'driverPath = r'C:\Users\29273\AppData\Local\Programs\Python\Python38-32\chromedriver.exe'driver = webdriver.Chrome(executable_path=driverPath)res = driver.find_elements_by_class_name("video-card")for i in range(len(res)): print('第' + str(i) + '个视频') print('up主:' + str(res[i].find_element_by_class_name("up-name__text").text)) print('视频名称:' + str(res[i].find_element_by_class_name("video-name").text)) print('视频播放量:' + str(res[i].find_element_by_class_name("play-text").text)) print('评论数:' + str(res[i].find_element_by_class_name("like-text").text)) print('note:' + str(res[i].find_element_by_class_name("history-hint").text))
上述内容就是selenium自动化测试的示例分析,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网行业资讯频道。