python爬虫入门(4)----- selenium
2024-10-09 09:05:14
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')
最新文章
- 25. Valid Palindrome
- java <;? super Fruit>;与<;? extends Fruit>;
- CF Amr and Pins (数学)
- weblogic集群无法启动,提示java.lang.NumberFormatException
- PRINTDLG 结构体
- Nginx学习——http配置项解析编程
- javascript中toString和valueOf方法的区别
- 批处理(Batch)---批处理脚本。
- [Inside HotSpot] Visual Studio2017编译调试openjdk12
- Active Directory Replication(复制)
- Linux 下面 Sqlserver 2017 的简单安装
- 搭建Hexo博客(一)-创建Hexo环境
- Kubernetes持久化存储1——示例
- 为何time_before 起作用【转】
- hbase与hive集成:hive读取hbase中数据
- JAVA基础知识总结:一到二十二全部总结
- [UE4]自定义结构体、类、数据表
- 移动端Css初始化
- poj2686 状压dp入门
- Java虚拟机的内存管理
热门文章
- Ueditor富文本添加视频内容,视频不显示以及编辑富文本时,视频不显示解决方案
- iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 3306 -j DNAT --to-destination 172.17.0.2:3306 ! -i docker0: iptables: No chain/target/match by that name
- day18__文件操作
- [javascript]js原型链以及原型链继承
- Eclipse配置maven环境1
- windows 下搭建 MQTT 服务
- [CEOI1999]Parity Game 题解
- Wooden Stricks——两个递增条件的线性DP
- HDU 3686 Traffic Real Time Query System (图论)
- day78 通过axios实现数据请求