python爬虫入门(4)----- selenium
selenium
简介
selenium使用JavaScript模拟真实用户对浏览器进行操作。测试脚本执行时,浏览器自动按照脚本代码做出点击,输入,打开,验证等操作,就像真实用户所做的一样,从终端用户的角度测试应用程序。
与python集成
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("su").click()
driver.quit()
selenium可以操纵各大主流浏览器chrome、firefox、ie等等,但需要下载相应的驱动包
chrome: http://chromedriver.storage.googleapis.com/index.html
firefox:https://github.com/mozilla/geckodriver/releases/
ie:http://selenium-release.storage.googleapis.com/index.html
webdriver(即:浏览器对象)基本使用方法
打开关闭标签页
#打开
def get(self, url) #关闭
def close(self) #退出浏览器
def quit(self)
设置浏览器宽高
def set_window_size(self, width, height, windowHandle='current'):
"""
Sets the width and height of the current window. (window.resizeTo) :Args:
- width: the width in pixels to set the window to
- height: the height in pixels to set the window to :Usage:
driver.set_window_size(800,600)
"""
对象定位
#通过id方式定位
driver.find_element_by_id("kw")#通过name方式定位
driver.find_element_by_name("wd") #通过tag name方式定位
driver.find_element_by_tag_name("input") #通过class name 方式定位
driver.find_element_by_class_name("s_ipt") #通过CSS方式定位
driver.find_element_by_css_selector("#kw") #通过xphan方式定位
driver.find_element_by_xpath("//input[@id='kw']") #通过link方式定位
driver.find_element_by_link_text("贴 吧") #Partial Link Text 定位
driver.find_element_by_partial_link_text("贴") #通过by指定方法类型定位
driver.find_element(By.ID, 'foo')
定位一组元素
#与上面类似加上s,但上面会抛出NoSuchElementException,下面找不到则返回empty list
#通过by指定方法类型定位
driver.find_elements(By.ID, 'foo')
框架和窗口定位
def switch_to(self):
"""
:Returns:
- SwitchTo: an object containing all options to switch focus into :Usage:
element = driver.switch_to.active_element
alert = driver.switch_to.alert
driver.switch_to.default_content()
driver.switch_to.frame('frame_name')
driver.switch_to.frame(1)
driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
driver.switch_to.parent_frame()
driver.switch_to.window('main')
"""
执行js
def execute_script(self, script, *args):
"""
Synchronously Executes JavaScript in the current window/frame. :Args:
- script: The JavaScript to execute.
- \*args: Any applicable arguments for your JavaScript. :Usage:
driver.execute_script('return document.title;')
""" def execute_async_script(self, script, *args):
"""
Asynchronously Executes JavaScript in the current window/frame. :Args:
- script: The JavaScript to execute.
- \*args: Any applicable arguments for your JavaScript. :Usage:
script = "var callback = arguments[arguments.length - 1]; " \
"window.setTimeout(function(){ callback('timeout') }, 3000);"
driver.execute_async_script(script)
"""
webelement(元素)基本使用方法
点击
driver.find_element_by_id("su").click()
driver.find_element_by_id("su").submit()
输入文本
driver.find_element_by_id("kw").send_keys("xxx")
获取属性/文本
driver.find_element_by_id("kw").text()
driver.find_element_by_id("kw").get_attribute()
driver.find_element_by_id("kw").get_property()
层次定位
#与webdiriver操作一样,可以以当前元素为父元素查找子元素
parent = driver.find_element(By.ID, 'parent')
parent.find_element(By.ID, 'child')
python爬虫入门(4)----- selenium的更多相关文章
- Python爬虫入门一之综述
大家好哈,最近博主在学习Python,学习期间也遇到一些问题,获得了一些经验,在此将自己的学习系统地整理下来,如果大家有兴趣学习爬虫的话,可以将这些文章作为参考,也欢迎大家一共分享学习经验. Pyth ...
- python爬虫入门-开发环境与小例子
python爬虫入门 开发环境 ubuntu 16.04 sublime pycharm requests库 requests库安装: sudo pip install requests 第一个例子 ...
- python爬虫动态html selenium.webdriver
python爬虫:利用selenium.webdriver获取渲染之后的页面代码! 1 首先要下载浏览器驱动: 常用的是chromedriver 和phantomjs chromedirver下载地址 ...
- Python爬虫入门教程 48-100 使用mitmdump抓取手机惠农APP-手机APP爬虫部分
1. 爬取前的分析 mitmdump是mitmproxy的命令行接口,比Fiddler.Charles等工具方便的地方是它可以对接Python脚本. 有了它我们可以不用手动截获和分析HTTP请求和响应 ...
- Python爬虫入门教程 43-100 百思不得姐APP数据-手机APP爬虫部分
1. Python爬虫入门教程 爬取背景 2019年1月10日深夜,打开了百思不得姐APP,想了一下是否可以爬呢?不自觉的安装到了夜神模拟器里面.这个APP还是比较有名和有意思的. 下面是百思不得姐的 ...
- Python 爬虫入门(二)——爬取妹子图
Python 爬虫入门 听说你写代码没动力?本文就给你动力,爬取妹子图.如果这也没动力那就没救了. GitHub 地址: https://github.com/injetlee/Python/blob ...
- Python爬虫入门之正则表达式
在前面我们已经搞定了怎样获取页面的内容,不过还差一步,这么多杂乱的代码夹杂文字我们怎样把它提取出来整理呢?下面就开始介绍一个十分强大的工具,正则表达式! 1.了解正则表达式 正则表达式是对字符串操作的 ...
- Python爬虫入门之Cookie的使用
本节我们一起来看一下Cookie的使用. 为什么要使用Cookie呢? Cookie,指某些网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密) 比如说有些网站需要 ...
- Python爬虫入门之Urllib库的高级用法
1.设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Headers 的属性. 首先,打开我们的浏览 ...
- Python爬虫入门之Urllib库的基本使用
那么接下来,小伙伴们就一起和我真正迈向我们的爬虫之路吧. 1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解 ...
随机推荐
- struct2面试准备
二 工作流程1.客户端浏览器发出HTTP请求.2.根据web.xml配置,该请求被FilterDispatcher接收3.根据struts.xml配置,找到需要调用的Action类和方法, 并通过Io ...
- hive命令查询数据不显示表头解决办法
在hive命令行中查询数据如下: 表头未显示出来 解决办法: 修改hive安装包conf/hive-site.xml配置文件: <property> <name>hive.cl ...
- SQL基础随记1 SQL分类 常用函数 ALL ANY EXISTS IN 约束
SQL基础随记1 SQL分类 常用函数 ALL ANY EXISTS IN 约束 其实这里知识不难,只是好久不接触突然被问的话有时还真的一时答不上,自己写一遍胜过盲扫.当然,也有些常读常新的地方会 ...
- Codeforces 1292C Xenon's Attack on the Gangs 题解
题目 On another floor of the A.R.C. Markland-N, the young man Simon "Xenon" Jackson, takes a ...
- Python-自动用0补取长度
描述 Python zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0. 语法 zfill()方法语法: str.zfill(width) 参数 width -- 指定字符串的长度. ...
- 工作那么久,才知道的 SOLID 设计原则
认识 SOLID 原则 无论是软件系统设计,还是代码实现,遵循有效和明确的设计原则,都利于系统软件灵活可靠,安全快速的落地,更重要的是能灵活地应对需求,简化系统扩展和维护,避免无效的加班.本文主要讨论 ...
- 在java中捕获异常时,使用log4j打印出错误堆栈信息
当java捕获到异常时,把详细的堆栈信息打印出来有助于我们排查异常原因,并修复相关bug,比如下面两张图,是打印未打印堆栈信息和打印堆栈信息的对比: 那么在使用log4j输出日志时,使用org.apa ...
- Blazor带我重玩前端(三)
写在前面 需要升级VS2019以及.NET Core到最新版(具体的最低支持,我已经忘了,总是越新支持的就越好),以更好的支持自己开发Blazor项目. WebAssembly 搜索Blazor模板 ...
- mysql全外和交叉&&sql92pksql99
#全外 use girls; SELECT b.*,a.* FROM beauty b FULL OUTER JOIN boys a on b.boyfrien_id=a.id; #交叉连接99标准笛 ...
- python中常见的数据类型
str 常用方法 1. 索引(下标) s = 'ABCDEFGHIJKLMN's1 = s[0]print('s[0] = ' + s1) #s[0] = A 2. 切片:顾头不顾尾 s = 'A ...