python Selenium 库的使用技巧


Posted in Python onOctober 16, 2020

Selenium 是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE,Mozilla Firefox,Safari,Google Chrome,Opera等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成 .Net、Java、Perl等不同语言的测试脚本。 -- 百度百科

首先下载驱动文件:https://chromedriver.storage.googleapis.com/index.html?path=2.39/

放入google目录下

python Selenium 库的使用技巧

测试代码,测试是否能读取到驱动文件。

from selenium import webdriver

path = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe"
driver = webdriver.Chrome(executable_path=path)

url = "https://www.baidu.com"
driver.get(url)
print(driver.page_source)

python Selenium 库的使用技巧

简单的实现浏览器测试

# -*- coding:utf-8 -*-
from selenium import webdriver

WebPath = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe"
driver = webdriver.Chrome(executable_path=WebPath)
driver.set_window_size(1000,500)

url = "https://www.baidu.com"
driver.get(url)
print(driver.find_element_by_id("kw"))

Selenium 自动化测试库的使用:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="gbk">
  <title>Selenium Test</title>
</head>
<body>
  <div class="acount" id="aid">
    <a class="mnav" href="https://news.baidu.com" rel="external nofollow" name="trnews">新闻</a>
    <a class="mnav" href="https://lyshark.cnblogs.com" rel="external nofollow" name="myblog">我的博客</a>
    <a class="mnav" href="https://github.com/lyshark" rel="external nofollow" name="mygit">GitHub</a>
  </div>
  <form id="forms" class="fms" name="submit_form" action="index.html">
    <span class="soutu-btn"></span>
    <p>用户: <input id="user" class="s_ipt" name="wd" value="" maxlength="255" autocomplete="off"></p>
    <p>密码: <input id="pass" class="s_ipt" name="wd" value="" maxlength="255" autocomplete="off"></p>
    <input type="submit" value="提交" />
  </form>
  <p name="p1" > hello lyshark p1</p>
  <p name="p2" > hello lyshark p2</p>
</body>
</html>

通过简单的浏览文件并实现简单的定位.

# 驱动下载地址: http://chromedriver.storage.googleapis.com/index.html
from selenium import webdriver

WebPath = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe"
driver = webdriver.Chrome(executable_path=WebPath)
driver.set_window_size(1024,768)

# 常用的定位变量参数如下所示.
driver.get("http://lyshark.com")
print("当前URL: {}".format(driver.current_url))
print("当前标题: {}".format(driver.title))
print("网页代码: {}".format(driver.page_source))

# 基本的 find_element 标签查找定位方式
print(driver.find_element_by_id("user"))     # 通过ID来查找元素
print(driver.find_element_by_name("p1").text)   # 通过name属性来定位
print(driver.find_element_by_class_name("s_ipt")) # 通过类名来定位

# 通过xpath定位,xpath定位有N种写法,这里列几个常用写法
print(driver.find_element_by_xpath("//form[@class='fms']//input[@id='user']"))
print(driver.find_element_by_xpath("//p[@name='p1']"))
print(driver.find_element_by_xpath("//html/body/form/p/input"))
print(driver.find_elements_by_css_selector(".fms #user"))

# 定位a标签中的关键字.
print(driver.find_element_by_link_text("新闻"))
print(driver.find_element_by_partial_link_text("我"))

通过xpath定位标签并自动输入内容,发送登录请求到后端,写法如下.

from selenium import webdriver

WebPath = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe"
driver = webdriver.Chrome(executable_path=WebPath)
driver.set_window_size(1024,768)

driver.get("http://lyshark.com")

# 通过xpath语法定位到用户名的标签上并且自动输入lyshark这个用户名
driver.find_element_by_xpath("//form[@class='fms']/p//input[@id='user']").send_keys("lyshark")

# 通过xpath语法定位到密码的标签上清空默认值,然后输入123123密码
driver.find_element_by_xpath("//form[@class='fms']/p//input[@id='pass']").clear()
driver.find_element_by_xpath("//form[@class='fms']/p//input[@id='pass']").send_keys("123123")

# 提交这个请求,默认有两种提交方式一种是 click() 一种是submit()
driver.find_element_by_xpath("//form[@class='fms']/input[@type='submit']").click()

通过键盘鼠标类库记录并可回放

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys

WebPath = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe"
driver = webdriver.Chrome(executable_path=WebPath)
driver.set_window_size(1024,768)

driver.get("https://www.baidu.com")

# ------------------------------------------------------------------------
# ActionChains 类提供了鼠标操作的常用方法,鼠标事件的常用函数说明
# perform():    鼠标悬浮于标签
# context_click(): 右击
# double_click():  双击
# drag_and_drop(): 拖动
# move_to_element():鼠标悬停

# 定位到要悬停的元素
above = driver.find_element_by_link_text("更多产品")
# 对定位到的元素执行鼠标悬停操作
ActionChains(driver).move_to_element(above).perform()

# ------------------------------------------------------------------------
# webdriver.common.keys 类提供了键盘事件的操作,以下为常用的键盘操作:
# send_keys(Keys.BACK_SPACE) 删除键(BackSpace)
# send_keys(Keys.SPACE) 空格键(Space)
# send_keys(Keys.TAB) 制表键(Tab)
# send_keys(Keys.ESCAPE) 回退键(Esc)
# send_keys(Keys.ENTER) 回车键(Enter)
# send_keys(Keys.CONTROL,'a') 全选(Ctrl+A)
# send_keys(Keys.CONTROL,'c') 复制(Ctrl+C)
# send_keys(Keys.CONTROL,'x') 剪切(Ctrl+X)
# send_keys(Keys.CONTROL,'v') 粘贴(Ctrl+V)
# send_keys(Keys.F1) 键盘 F1

# 输入框输入内容
driver.find_element_by_id("kw").send_keys("seleniumm")
# 删除多输入的一个 m
driver.find_element_by_id("kw").send_keys(Keys.BACK_SPACE)
# 输入空格键+从入门到入土
driver.find_element_by_id("kw").send_keys(Keys.SPACE)
driver.find_element_by_id("kw").send_keys("从入门到入土")

# ctrl+a 全选输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'a')
# ctrl+x 剪切输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'x')
# ctrl+v 粘贴内容到输入框
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'v')

# 通过回车键来代替单击操作
driver.find_element_by_id("su").send_keys(Keys.ENTER)

简单的点击事件

# -*- coding:utf-8 -*-
from selenium import webdriver
import time

WebPath = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe"
driver = webdriver.Chrome(executable_path=WebPath)
driver.set_window_size(1024,768)
driver.get("https://www.baidu.com")

driver.find_element_by_id("kw").send_keys("lyshark") # 发送给id=kw的编辑框,搜索关键字 lyshark
driver.find_element_by_id("su").click()        # 点击搜索按钮,百度一下的ID是su
time.sleep(1)
# xpath 语法 寻找 div id是1里面的 a标签取出标签中的 contains text()
driver.find_element_by_xpath("//div[@id='1']//a[contains(text(),'-')]").click()
time.sleep(1)

handle = driver.current_window_handle  # 获取当前窗口句柄
handle_all = driver.window_handles   # 获取当前所有开启窗口的句柄
print(handle_all)
driver.switch_to.window(handle_all[0])  # 切换到第一个窗口中
time.sleep(1)
driver.find_element_by_id("kw").clear() # 接着清空搜索框中的内容

python Selenium 库的使用技巧

百度自动收集

from selenium import webdriver
from bs4 import BeautifulSoup
from queue import Queue
import requests,os,re,lxml

# driver: http://chromedriver.storage.googleapis.com/index.html?path=79.0.3945.36/

head = {"User-Agent":"Mozilla/5.0 (iPhone; U; CPU like Mac OS X) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3"}
WebPath = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe"
driver = webdriver.Chrome(executable_path=WebPath)


queue = Queue()
for item in range(0,1000,10):
	queue.put('https://www.baidu.com/s?wd={}&pn={}'.format("lyshark",str(item)))

for item in queue.queue:
	driver.get(item)
	ret = str(driver.page_source)
	try:
		soup = BeautifulSoup(ret,'lxml')
		urls = soup.find_all(name='a',attrs={'data-click':re.compile(('.')),'class':None})
		for item in urls:
		  get_url = requests.get(url=item['href'],headers=head,timeout=5)
		  if get_url.status_code == 200:
		    print(get_url.url)
	except Exception:
		pass

python Selenium 库的使用技巧

页面等待

from selenium import webdriver

driver=webdriver.Chrome()
driver.get('https://www.taobao.com/')
wait=WebDriverWait(driver,3) #设置监听driver等待时间3秒
input=wait.until(EC.presence_of_element_located((By.ID,'q'))) #设置等待条件为id为q的元素加载完成
button=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.btn-search'))) #设置等待条件为class名为btn-search的元素加载完成
print(input,button)

driver = webdriver.Firefox()
driver.implicitly_wait(10) #隐式等待设置为10等待时间
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")

键盘操作

element=driver.find_element_by_id('search') #获取输入框
element.send_keys('selenium') #搜索selenium包
element.send_keys(Keys.ENTER) #按回车键

element_a=driver.find_element_by_link_text('selenium') #定位selenium包链接

ActionChains(driver).move_to_element(element_a).click(element_a).perform() #按左键点击链接执行

element_down=driver.find_element_by_link_text('Download files') #定位下载链接
ActionChains(driver).move_to_element(element_down).click(element_down).perform() #按左键点击链接

element_selenium=driver.find_element_by_link_text('selenium-3.13.0.tar.gz') #定位元素selenium下载包链接
data=element_selenium.get_attribute('href')  #获取链接地址
with open('selenium-3.13.0.tar.gz','wb') as f:
  source=requests.get(data).content  #请求下载链接地址获取二进制包数据
  f.write(source) #写入数据
  f.close()
  
driver.quit()

menu = driver.find_element_by_css_selector(".nav") #获取element对象
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1") #获取点击对象
#创建鼠标对象
actions = ActionChains(driver)
#移动鼠标到对象
actions.move_to_element(menu)
#点击对象
actions.click(hidden_submenu)
#执行操作
actions.perform()

文章作者:lyshark
文章出处:https://www.cnblogs.com/lyshark

以上就是python Selenium 库的使用技巧的详细内容,更多关于python Selenium 库的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
用pywin32实现windows模拟鼠标及键盘动作
Apr 22 Python
Python中的包和模块实例
Nov 22 Python
Python中转换角度为弧度的radians()方法
May 18 Python
Python编程中装饰器的使用示例解析
Jun 20 Python
Python实现批量读取图片并存入mongodb数据库的方法示例
Apr 02 Python
pandas计算最大连续间隔的方法
Jul 04 Python
windows上安装python3教程以及环境变量配置详解
Jul 18 Python
解决django中ModelForm多表单组合的问题
Jul 18 Python
pygame实现打字游戏
Feb 19 Python
Python爬虫库BeautifulSoup的介绍与简单使用实例
Jan 25 Python
python 模拟登陆github的示例
Dec 04 Python
python 写一个文件分发小程序
Dec 05 Python
用Python进行websocket接口测试
Oct 16 #Python
python如何控制进程或者线程的个数
Oct 16 #Python
python利用 keyboard 库记录键盘事件
Oct 16 #Python
python实现快速文件格式批量转换的方法
Oct 16 #Python
Python通过getattr函数获取对象的属性值
Oct 16 #Python
pandas处理csv文件的方法步骤
Oct 16 #Python
python爬取”顶点小说网“《纯阳剑尊》的示例代码
Oct 16 #Python
You might like
php环境无法上传文件的解决方法
2014/04/30 PHP
php实现的任意进制互转类分享
2015/07/07 PHP
php实现的redis缓存类定义与使用方法示例
2017/08/09 PHP
通过实例解析PHP数据类型转换方法
2020/07/11 PHP
Jquery Ajax 学习实例2 向页面发出请求 返回JSon格式数据
2010/03/15 Javascript
用js来定义浏览器中一个左右浮动元素相对于页面主体宽度的位置的函数
2012/01/21 Javascript
JS图片根据鼠标滚动延时加载的实例代码
2013/07/13 Javascript
js实现弹窗插件功能实例代码分享
2013/12/12 Javascript
js生成随机数的方法实例
2015/10/16 Javascript
javascript实现表单验证
2016/01/29 Javascript
js阻止浏览器默认行为触发的通用方法(推荐)
2016/05/15 Javascript
js事件源window.event.srcElement兼容性写法(详解)
2016/11/25 Javascript
浅谈js-FCC算法Friendly Date Ranges(详解)
2017/04/10 Javascript
seajs下require书写约定实例分析
2018/05/16 Javascript
JavaScript 中的12种循环遍历方法【总结】
2018/05/31 Javascript
javascript获取select值的方法完整实例
2019/06/20 Javascript
JavaScript或jQuery 获取option value值方法解析
2020/05/12 jQuery
微信小程序使用GoEasy实现websocket实时通讯
2020/05/19 Javascript
Vue双向数据绑定(MVVM)的原理
2020/10/03 Javascript
[01:18:35]DOTA2-DPC中国联赛 正赛 Elephant vs LBZS BO3 第一场 1月29日
2021/03/11 DOTA
Python使用cx_Oracle模块将oracle中数据导出到csv文件的方法
2015/05/16 Python
Python下Fabric的简单部署方法
2015/07/14 Python
为什么选择python编程语言入门黑客攻防 给你几个理由!
2018/02/02 Python
python写一个md5解密器示例
2018/02/23 Python
在pycharm 中添加运行参数的操作方法
2019/01/19 Python
python pandas写入excel文件的方法示例
2019/06/25 Python
Python基于codecs模块实现文件读写案例解析
2020/05/11 Python
英国电子产品购物网站:TobyDeals
2018/07/30 全球购物
Converse匡威法国官网:美国著名帆布鞋品牌
2018/12/05 全球购物
企业演讲稿范文
2013/12/28 职场文书
小学新学期寄语
2014/04/02 职场文书
国际商务专业求职信
2014/07/15 职场文书
庆六一文艺汇演活动方案
2014/08/26 职场文书
导航工程专业自荐信
2014/09/02 职场文书
社区班子个人对照检查材料思想汇报
2014/10/07 职场文书
小学语文课《掌声》教学反思
2016/03/03 职场文书