笔记-爬虫-selenium常用方法
笔记-爬虫-selenium常用方法
1. 查找元素
常用的查找方法
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
也可以使用通用的方法
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
input_first = browser.find_element(BY.ID,'q')#第一个参数传入名称,第二个传入具体的参数
print(input_first)
browser.close()
查找多个元素,elements多个s
input_first = browser.find_elements_by_id('q')
2. 交互操作
2.1. 操作浏览器
|
方法 |
说明 |
|
set_window_size() |
设置浏览器的大小 |
|
back() |
控制浏览器后退 |
|
forward() |
控制浏览器前进 |
|
refresh() |
刷新当前页面 |
|
submit() |
用于提交表单 |
|
close() |
关闭单个窗口 |
|
quit() |
关闭所有窗口 |
|
get_screenshot_as_file(self, filename) |
用于截取当前窗口,并把图片保存到本地 |
|
driver.switchTo().window("windowName") |
在Windows之间移动 |
|
driver.switchTo().frame("frameName"); |
在 frame 之间移动 |
注:frame相当于独立的网页,如果在父类网frame查找子类的,则必须切换到子类的frame,子类如果查找父类也需要先切换
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
browser = webdriver.Chrome()
url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'
browser.get(url)
browser.switch_to.frame('iframeResult')
source = browser.find_element_by_css_selector('#draggable')
print(source)
try:
logo = browser.find_element_by_class_name('logo')
except NoSuchElementException:
print('NO LOGO')
browser.switch_to.parent_frame()
logo = browser.find_element_by_class_name('logo')
print(logo)
print(logo.text)
2.2. 元素操作
|
clear() |
清除文本 |
|
send_keys(*value) |
模拟按键输入/赋值 |
|
click() |
单击元素 |
|
current_url |
获取当前页面url |
|
location |
元素坐标 |
|
get_attribute(element_name) |
获取元素属性值 |
|
is_selected() |
|
|
size |
元素大小 |
|
is_displayed() |
|
|
is_enabled() |
|
|
text |
元素文本值 |
|
tagName |
元素的tagName |
2.3. 鼠标操作
这个需要使用webdriver下的ActionChains类,这个类是操作鼠标操作的:
from selenium.webdriver import ActionChains
鼠标操作可分为三类:鼠标移动、鼠标拖拽、鼠标点击
element = driver.find_element_by_name('tj_settingicon')
#鼠标点击
ActionChains(driver).click(element).perform() #单击某元素
ActionChains(driver).click_and_hold(element).perform() #在此元素上按下左键不放
ActionChains(driver).context_click(element).perform() #在此元素上单击右键
ActionChains(driver).double_click(element).perform() #在此元素上双击
#鼠标拖拽
ActionChains(driver).drag_and_drop(source,target).perform() #从一个元素的位置,拖至另一个元素位置松开
ActionChains(driver).drag_and_drop_by_offset(source,xoffset,yoffset) #以坐标的形式拖拽,x,y
#鼠标移动
ActionChains(driver).move_by_offset(x,y) #移动到(x,y)坐标位置
ActionChains(driver).move_to_element(element) #鼠标移动到某个元素上
ActionChains(driver).move_to_element_with_offset(element,x,y) #移动到某个元素上,然后,在移动到相对坐标(x,y)上
2.4. 执行JavaScript
有些动作可能没有提供api,比如进度条下拉,这时,我们可以通过代码执行JavaScript。
webdriver 提供了 execute_script() 接口用来调用 js 代码。
执行 JS 一般有两种场景:
1:一种是在页面上直接执行 JS
2:另一种是在某个已经定位的元素上执行 JS
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.zhihu.com/explore')
browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')
browser.execute_script('alert("To Bottom")')
2.5. 等待
隐式等待
当使用了隐式等待执行测试的时候,如果 WebDriver没有在 DOM中找到元素,将继续等待,超出设定时间后则抛出找不到元素的异常,
换句话说,当查找元素或元素并没有立即出现的时候,隐式等待将等待一段时间再查找 DOM,默认的时间是0
browser.implicitly_wait(10)#等待十秒加载不出来就会抛出异常,10秒内加载出来正常返回
browser.get('https://www.zhihu.com/explore')
input = browser.find_element_by_class_name('zu-top-add-question')
显式等待
指定一个等待条件,和一个最长等待时间,程序会判断在等待时间内条件是否满足,如果满足则返回,如果不满足会继续等待,超过时间就会抛出异常
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
browser.get('https://www.taobao.com/')
wait = WebDriverWait(browser, 10)
input = wait.until(EC.presence_of_element_located((By.ID, 'q')))
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-search')))
print(input, button)
until()
WebDriverWait()一般由until()(或until_not())方法配合使用,下面是until()和until_not()方法的说明。
until(method, message=’ ’)
调用该方法提供的驱动程序作为一个参数,直到返回值为Ture。
until_not(method, message=’ ’)
调用该方法提供的驱动程序作为一个参数,直到返回值为False。
Expected Conditions
在本例中,我们在使用expected_conditions 类时对其时行了重命名,通过as 关键字对其重命名为EC,
并调用presence_of_element_located()判断元素是否存在。
expected_conditions 类提供一些预期条件的实现。
title_is 用于判断标题是否xx。
title_contains 用于判断标题是否包含xx 信息。
presence_of_element_located 元素是否存在。
visibility_of_element_located 元素是否可见。
visibility_of 是否可见
presence_of_all_elements_located 判断一组元素的是否存在
text_to_be_present_in_element 判断元素是否有xx 文本信息
text_to_be_present_in_element_value 判断元素值是否有xx 文本信息
frame_to_be_available_and_switch_to_it 表单是否可用,并切换到该表单。
invisibility_of_element_located 判断元素是否隐藏
element_to_be_clickable 判断元素是否点击,它处于可见和启动状态
staleness_of 等到一个元素不再是依附于DOM。
element_to_be_selected 被选中的元素。
element_located_to_be_selected 一个期望的元素位于被选中。
element_selection_state_to_be 一个期望检查如果给定的元素被选中。
element_located_selection_state_to_be 期望找到一个元素并检查是否选择状态
alert_is_present 预期一个警告信息
除了expected_conditions 所提供的预期方法,我们也可以使用前面学过的is_displayed()方法来判断元素是否可。
2.6. Cookies
|
driver.get_cookies() |
|
|
driver.get_cookies() |
|
|
delete_all_cookies() |
|
|
delete_cookie(name) |
|
|
add_cookie(cookie_dict) |
添加 cookie,必须有 name 和 value 值 |
2.7. 异常处理
from selenium import webdriver
from selenium.common.exceptions import TimeoutException, NoSuchElementException
browser = webdriver.Chrome()
try:
browser.get('https://www.baidu.com')
except TimeoutException:
print('Time Out')
try:
browser.find_element_by_id('hello')
except NoSuchElementException:
print('No Element')
finally:
browser.close()
笔记-爬虫-selenium常用方法的更多相关文章
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍
这篇文章主要Selenium+Python自动测试或爬虫中的常见定位方法.鼠标操作.键盘操作介绍,希望该篇基础性文章对你有所帮助,如果有错误或不足之处,请海涵~同时CSDN总是屏蔽这篇文章,再加上最近 ...
- 笔记-爬虫-去重/bloomfilter
笔记-爬虫-去重/bloomfilter 1. 去重 为什么要去重? 页面重复:爬的多了,总会有重复的页面,对已爬过的页面肯定不愿意再爬一次. 页面更新:很多页面是会更新的,爬取这种页面时就 ...
- 笔记-爬虫-robots.txt
笔记-爬虫-robots.txt 1. robots.txt文件简介 1.1. 是什么 robots.txt是用来告诉搜索引擎网站上哪些内容可以被访问.哪些不能被访问.当搜索引擎访问一 ...
- [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...
- [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论
前面几篇文章介绍了Selenium.PhantomJS的基础知识及安装过程,这篇文章是一篇应用.通过Selenium调用Phantomjs获取CSDN下载资源的信息,最重要的是动态获取资源的评论,它是 ...
- [Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒
前面我讲述过如何通过BeautifulSoup获取维基百科的消息盒,同样可以通过Spider获取网站内容,最近学习了Selenium+Phantomjs后,准备利用它们获取百度百科的旅游景点消息盒(I ...
- [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)
转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...
- python爬虫---selenium库的用法
python爬虫---selenium库的用法 selenium是一个自动化测试工具,支持Firefox,Chrome等众多浏览器 在爬虫中的应用主要是用来解决JS渲染的问题. 1.使用前需要安装这个 ...
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍(转载)
转载地址:[python爬虫] Selenium常见元素定位方法和操作的学习介绍 一. 定位元素方法 官网地址:http://selenium-python.readthedocs.org/locat ...
随机推荐
- 浅谈position、table-cell、flex-box三种垂直(水平)居中技巧
一.首先是喜闻乐见的position方法,经典且万能,用法如下: 父元素{ position:relative; } 子元素{ position:absolute; top:50%; left:50% ...
- jquery-validate插件
jQuery Validation 插件 优点:1.表单验证非常简单方便,并且提供了许多配置项目2.国际化,可以自定义提示信息 命令行安装 //初始化bowerbower init //使用bower ...
- u-boot分析(五)----I/D cache失效|关闭MMU和cache|关闭看门狗
u-boot分析(五) 上篇博文我们按照210的启动流程,对u-boot启动中的设置异常向量表,设置SVC模式进行了分析,今天我们继续按照u-boot的启动流程对以下内容进行分析. 今天我们会用到的文 ...
- Eclipse 如何修改 Web 项目的名称
Eclipse 切换到 Navigator 视图,能显现出项目下所有的文件便于修改. 1.修改该项目目录下:.project文件 <projectDescription><name ...
- 创建Gradle工程出现Could not install Gradle distribution from 'https://services.gradle.org/distributions/gradleXX'.问题解决
在 Eclipse EE Oxygen 中创建 Gradle Project的时候 出现如下错误: org.gradle.tooling.GradleConnectionException: Coul ...
- Spark远程调试函数
Spark远程调试函数 1.sendInfo 该函数用于分布式程序的调试,非常方便,在spark的rdd操作中嵌入sendInfo,启动nc服务器后,可以收集到所有需要的运行时信息,该函数可以捕获ho ...
- PAT1137
题意 一个学生的成绩由上机,期中,期末共3部分构成,现要求找出有资格获得证书的同学们. 证书获得者要求:上机分至少200,最终成绩及格. 最终成绩的生成规则:若期中分>期末分,则f = 期中 * ...
- 洛谷 P1215 [USACO1.4]母亲的牛奶 Mother's Milk
题目描述 农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数, 最初,A和B桶都是空的,而C桶是装满牛奶的.有时,农民把牛奶从一个桶倒到另一个桶中,直到被灌桶装满或原桶空了 ...
- Windows Host 文件
Windows XP Home / Windows 7/ Windows Server 2008 c:\windows\system32\drivers\etc\hosts 如果碰到Localhost ...
- Vue--父组件传数据给子组件,子组件生命周期过程拿到数据的情况
需求: 在子组件渲染之前,我要修改数据的某个字段 结果是 组件在beforeUpdate,updated 的状态才能拿到父组件的数据 那么证明,我根本无法在beforeUpdate,updated两个 ...