Python selenium 滚动条 详解
在我们使用Python + selenium 爬虫的时候,会遇到如下报错,原因是 当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到,会报元素不可见的。
selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (, )
这时候需要借助滚动条来拖动屏幕,使被操作的元素显示在当前的屏幕上。滚动条是无法直接用定位工具来定位的。selenium里面也没有直接的方法去控制滚动条,
这时候只能借助Js了,还好selenium提供了一个操作js的方法:
execute_script(),可以直接执行js的脚本。
一 、控制滚动条高度
1.滚动条回到顶部:
js="var q=document.getElementById(‘id‘).scrollTop=0"
driver.execute_script(js)
.滚动条拉到底部 js="var q=document.documentElement.scrollTop=10000"
driver.execute_script(js) .这里可以修改scrollTop 的值,来定位右侧滚动条的位置,0是最上面,10000是最底部。
js="var q=document.documentElement.scrollTop=5000"
driver.execute_script(js)
二、控制横向滚动条
1.有时候浏览器页面需要左右滚动(一般屏幕最大化后,左右滚动的情况已经很少见了)。
.通过左边控制横向和纵向滚动条scrollTo(x, y) js = "window.scrollTo(100,400);"
driver.execute_script(js)
.第一个参数x是横向距离,第二个参数y是纵向距离
附:
# 移动到元素element对象的“顶端”与当前窗口的“顶部”对齐
driver.execute_script("arguments[0].scrollIntoView();", element);
driver.execute_script("arguments[0].scrollIntoView(true);", element); # 移动到元素element对象的“底端”与当前窗口的“底部”对齐
driver.execute_script("arguments[0].scrollIntoView(false);", element); # 移动到页面最底部
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)"); # 移动到指定的坐标(相对当前的坐标移动)
driver.execute_script("window.scrollBy(0, 700)");
# 结合上面的scrollBy语句,相当于移动到700+=1600像素位置
driver.execute_script("window.scrollBy(0, 800)"); # 移动到窗口绝对位置坐标,如下移动到纵坐标1600像素位置
driver.execute_script("window.scrollTo(0, 1600)");
# 结合上面的scrollTo语句,仍然移动到纵坐标1200像素位置
driver.execute_script("window.scrollTo(0, 1200)");
Python selenium 滚动条 详解的更多相关文章
- selenium用法详解
		
selenium用法详解 selenium主要是用来做自动化测试,支持多种浏览器,爬虫中主要用来解决JavaScript渲染问题. 模拟浏览器进行网页加载,当requests,urllib无法正常获取 ...
 - python爬虫知识点详解
		
python爬虫知识点总结(一)库的安装 python爬虫知识点总结(二)爬虫的基本原理 python爬虫知识点总结(三)urllib库详解 python爬虫知识点总结(四)Requests库的基本使 ...
 - Python 字符串方法详解
		
Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. ...
 - python time模块详解
		
python time模块详解 转自:http://blog.csdn.net/kiki113/article/details/4033017 python 的内嵌time模板翻译及说明 一.简介 ...
 - Python中dict详解
		
from:http://www.cnblogs.com/yangyongzhi/archive/2012/09/17/2688326.html Python中dict详解 python3.0以上,pr ...
 - Python开发技术详解(视频+源码+文档)
		
Python, 是一种面向对象.直译式计算机程序设计语言.Python语法简捷而清晰,具有丰富和强大的类库.它常被昵称为胶水语言,它能够很轻松的把用其他语言制作的各种模块(尤其是C/C++)轻松地联结 ...
 - python/ORM操作详解
		
一.python/ORM操作详解 ===================增==================== models.UserInfo.objects.create(title='alex ...
 - 【python进阶】详解元类及其应用2
		
前言 在上一篇文章[python进阶]详解元类及其应用1中,我们提到了关于元类的一些前置知识,介绍了类对象,动态创建类,使用type创建类,这一节我们将继续接着上文来讲~~~ 5.使⽤type创建带有 ...
 - Python开发技术详解PDF
		
Python开发技术详解(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1F5J9mFfHKgwhkC5KuPd0Pw 提取码:xxy3 复制这段内容后打开百度网盘手 ...
 
随机推荐
- 微信小程序 - 骨架屏
			
骨架屏 - “与其等待网络加载,不如提前给点暗示” 注:不适用复杂交互效果 演示 示例解释以及使用全在index.wxml中,观看需了解组件使用. 示例下载:微信小程序-骨架屏演示
 - Want to write a book? Use word count to stay on track
			
http://paloalto.patch.com/groups/maria-murnanes-blog/p/bp--want-to-write-a-book-use-word-count-to-st ...
 - iSCSI Network Designs: Part 5 – iSCSI Multipathing, Host Bus Adapters, High Availability and Redundancy
			
iSCSI Network Designs: Part 5 – iSCSI Multipathing, Host Bus Adapters, High Availability and Redunda ...
 - vsphere性能
			
vNUMA介绍 http://virtualbarker.com/ vSphere VMware Performance With every release of vSphere the overh ...
 - Mysql官方文档中争对安全添加列的处理方法。Mysql Add a Column to a table if not exists
			
Add a Column to a table if not exists MySQL allows you to create a table if it does not exist, but d ...
 - SHELL AWK 循环求和
			
1.简单求和,文件如下: [linux@test /tmp]$ cat test 123.52 125.54 126.36 求和: [linux@test /tmp]$ awk '{sum += $1 ...
 - vue重要特性
			
重要特性 自定义input组件 动态组件 递归组件 slot 作用域slot 异步组件 内联模板 子组件索引 进阶 自定义指令 状态管理vuex 单文件组件 生产部署 路由 xxx
 - angularjs中templateUrl的路径问题
			
angularjs中templateUrl的路径问题 templateUrl的相对路径是现对于app主页面的,因为template使用js来加载的.
 - Aerospike系列:7:数据分布详解
			
1:Aerospike数据库是Shared-Nothing架构,集群中的每个节点都是相同的,不会出现单点故障. Aerospike有智能分区算法,即把用户输入的key在内部根据RIPEMD-160算法 ...
 - Response的返回内容类型
			
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6682514.html 服务器在返回结果给浏览器时,通常需要先设置响应头的contentType属性.那么,c ...