(—)滚动条操作

python中selenium操作下拉滚动条方法汇总

selenium_webdriver(python)控制浏览器滚动条

selenium+Python(select定位)

Selenium+Python常见定位方法

selenium_webdriver(python)元素定位详解

Selenium+Python浏览器滚动条操作

elenium+python配置chrome浏览器的选项

#(1)使用JS操作浏览器(右侧竖向)滚动条
time.sleep(3)#最好等个几秒
#将滚动条移动到页面的顶部
js="var q=document.documentElement.scrollTop=0"
driver.execute_script(js)
#页面内嵌窗口浏览条滚动
js="var q=document.getElementById('id').scrollTop=1000"
driver.execute_script(js)
time.sleep(3)
# 通过按向下键将页面滚动条拖到底部
driver.find_element_by_xpath("xpath").send_keys(Keys.DOWN)#(2)使用JS操作浏览器(底部横向)滚动条
#这个是全页面的
js = "window.scrollTo(210,550);" #(x, y)
x:代表横向坐标
y:代表纵向坐标
#(3)当我们需要定位的元素是动态元素,或者我们不确定它在哪时,可以先找到这个元素然后再使用JS操作
target = driver.find_element_by_id('id')
driver.execute_script("arguments[0].scrollIntoView();", target)

使用xpath解决一切复杂的定位。

find_element_by_xpath("//span[text()='APPRMIN']")
find_elements_by_xxx 会返回一个所有符合条件的element组成的列表,通过element.text属性获取文本texts = list(ele.text for ele in find_elements_by_xpath("//span[@class='tree_title']"))

======以下是Select定位====

1、Select元素

1.打开百度-设置-搜索设置界面,如下图所示

2.箭头所指位置,就是 select 选项框,打开页面元素定位,下方红色框框区域,可以看到 select 标签属性:

<select id="nr" name="NR">
3.选项有三个
<option selected="" value="10">每页显示 10 条</option>
<option value="20">每页显示 20 条</option>
<option value="50">每页显示 50 条</option>

2、定位select

定位select有多种方法,下面进行一一介绍

2.1 二次定位

1.定位 select 里的选项有多种方式,这里先介绍一种简单的方法:二次定位
2.基本思路,先定位 select 框,再定位 select 里的选项

完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.get("https://www.baidu.com/")
driver.implicitly_wait(20)
mouse = driver.find_element_by_link_text("设置")
ActionChains(driver).move_to_element(mouse).perform()
driver.find_element_by_link_text("搜索设置").click()
= driver.find_element_by_id("nr")
s.find_element_by_xpath("//option[@value='50']").click()
# 二次定位另外一种写法
driver.find_element_by_id("nr").find_element_by_xpath("//option[@value='50']").click()

3.还有另外一种写法也是可以的,把最下面两步合并成为一步:

driver.find_element_by_id("nr").find_element_by_xpath("//option[@value='50']").click()

2.2 直接定位

1.有很多小伙伴说 firebug 只能定位到 select 框,还能定位里面的选项。
2.用 direbug 定位到 select 后,下方查看元素属性地方,点 select 标签前面的+号,就可以展开里面的选项内容了。

3.然后自己写 xpath 定位或者 css,一次性直接定位到 option 上的内容。

完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.get("https://www.baidu.com/")
driver.implicitly_wait(20)
mouse = driver.find_element_by_link_text("设置")
ActionChains(driver).move_to_element(mouse).perform()
driver.find_element_by_link_text("搜索设置").click()
# 直接点位
driver.find_element_by_xpath(".//*[@id='nr']/option[2]").click()

2.3  Select  模块(index)点位  

1.除了上面介绍的两种简单的方法定位到 select 选项,selenium 还提供了更高级的玩法,导入 Select 模块。直接根据属性或索引定位。
2.先要导入 select 方法:
from selenium.webdriver.support.select import Select
3.然后通过 select 选项的索引来定位选择对应选项(从 0 开始计数),如选择第三个选项:select_by_index(2)
   完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
driver = webdriver.Firefox()
driver.get("https://www.baidu.com/")
driver.implicitly_wait(20)
mouse = driver.find_element_by_link_text("设置")
ActionChains(driver).move_to_element(mouse).perform()
driver.find_element_by_link_text("搜索设置").click()
# 通过索引:select_by_index()
= driver.find_element_by_id("nr")
Select(s).select_by_index(2)

2.4 Select  模块(value)定位

1.Select 模块里面除了 index 的方法,还有一个方法,通过选项的 value值来定位。每个选项,都有对应的 value 值,如
<select id="nr" name="NR">
<option selected="" value="10">每页显示 10 条</option>
<option value="20">每页显示 20 条</option>
<option value="50">每页显示 50 条</option>
2.第二个选项对应的 value 值就是“20”:select_by_value(2)

完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
driver = webdriver.Firefox()
driver.get("https://www.baidu.com/")
driver.implicitly_wait(20)
mouse = driver.find_element_by_link_text("设置")
ActionChains(driver).move_to_element(mouse).perform()
driver.find_element_by_link_text("搜索设置").click()
# 通过value定位:select_by_value()
= driver.find_element_by_id("nr")
Select(s).select_by_value(20)

2.5  Select  模块(text)定位

1.Select 模块里面还有一个更加高级的功能,可以直接通过选项的文本内容来定位。
2.定位“每页显示 50 条”:select_by_visible_text("每页显示 50 条")

完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
driver = webdriver.Firefox()
driver.get("https://www.baidu.com/")
driver.implicitly_wait(20)
mouse = driver.find_element_by_link_text("设置")
ActionChains(driver).move_to_element(mouse).perform()
driver.find_element_by_link_text("搜索设置").click()
# 通过select_by_visible_text定位
= driver.find_element_by_id("nr")
Select(s).select_by_visible_text("每页显示50条")

3.Select  模块其它方法

1.select 里面方法除了上面介绍的三种,还有更多的功能如下

select_by_index() :通过索引定位
select_by_value() :通过 value 值定位
select_by_visible_text() :通过文本值定位
deselect_all() :取消所有选项
deselect_by_index() :取消对应 index 选项
deselect_by_value() :取消对应 value 选项
deselect_by_visible_text() :取消对应文本选项
first_selected_option() :返回第一个选项
all_selected_options() :返回所有的选项

转载于:https://www.cnblogs.com/mapu/p/9014318.html

selenium python 一些操作和定位收集相关推荐

  1. python实时定位_selenium python 一些操作和定位收集

    1.Select元素 1.打开百度-设置-搜索设置界面,如下图所示 2.箭头所指位置,就是 select 选项框,打开页面元素定位,下方红色框框区域,可以看到 select 标签属性: 3.选项有三个 ...

  2. selenium python (八)定位frame中的对象

    #!/usr/bin/python # -*- coding: utf-8 -*- __author__ = 'zuoanvip' #在测试过程中经常遇到frame嵌套的应用,加入页面上有A.B两个f ...

  3. python学习的一个定位_python学习之——selenium元素定位

    web自动化测试按步骤拆分,可以分为四步操作:定位元素,操作元素,获取返回结果,断言(返回结果与期望结果是否一致),最后自动出测试报告. 其中定位元素尤为关键,此篇是使用webdriver通过页面各个 ...

  4. python web自动化元素定位_快速掌握Python Selenium Web自动化:三)在Selenium中定位查找网页元素的诸类方法...

    使用Selenium进行自动化操作,首先要做的就是通过webdriver的get()方法打开一个URL链接. 在打开链接,完成页面加载之后,就可以通过Selenium提供的接口,在页面上进行各种操作了 ...

  5. Selenium+Python如何定位鼠标悬停的元素

    Selenium+Python如何定位鼠标悬停的元素? 下面以登录百度后,鼠标悬停在个人中心,点击[退出登录]为例. Selenium+Python如何定位鼠标悬停的元素? 一.环境 二.代码 三.定 ...

  6. 已解决python selenium模块自动化操作谷歌浏览器点击元素失效问题解决(亲测有效)

    已解决python selenium模块自动化操作浏览器点击元素,抛出异常selenium.common.exceptions.ElementClickInterceptedException: Me ...

  7. 定位到元素后获取其属性_Selenium界面自动化测试(4)(Python):元素定位及操作...

    在操作Web元素之前,需要先找到该元素,这个查找的过程称之为元素定位. Selenium支持8种元素定位方法: ID:根据元素的id属性值来定位元素. Name:根据元素的name属性值来定位元素. ...

  8. selenium+python,解决selenium弹出新页面,无法定位元素的问题(报错:Unable to locate element:元素)

    selenium+python,解决selenium弹出新页面,无法定位元素的问题(报错:Unable to locate element:元素) 参考文章: (1)selenium+python,解 ...

  9. seleniumpython定位网页元素方法_使用Selenium对网页元素进行定位的诸种方法

    使用Selenium进行自动化操作,首先要做的就是通过webdriver的get()方法打开一个URL链接. 在打开链接,完成页面加载之后,就可以通过Selenium提供的接口,在页面上进行各种操作了 ...

最新文章

  1. 理财周报,致歉还远远不够
  2. 【IBM Tivoli Identity Manager 学习文档】15 用户管理
  3. c++标准模板库STL【快速查找】【最全】【常用】【语法】
  4. 介绍神经网络_神经网络介绍
  5. 微软Windows Community Toolkit一览
  6. 精通 RPM 之查询篇
  7. 三、Express 路由
  8. JavaEE基础(十七)/集合
  9. Yii 一些小的问题
  10. css3-10 css3中的边框样式有哪几种
  11. opencl icd---OpenCL Installable Client Driver (ICD) Loader
  12. java实现视频播放背景_使用VideoJS React的全屏背景视频
  13. 【Verilog语法简介】
  14. 尝试用朴素贝叶斯分析借款信用等级
  15. 中国古代文化常识【1】
  16. PHP绘制正方形印章,ps绘制一款正方形的个人印章的方法
  17. 电脑连接wifi后,显示 无Internet 安全
  18. 华硕主板M2N-电源跳线怎么接
  19. 伯禹公益AI《动手学深度学习PyTorch版》Task 05 学习笔记
  20. 中式红木整装塑造出中式家居的浪漫氛围

热门文章

  1. c语言各种编程风格 微软 gnu,编程规范-c语言的编程风格
  2. 在r中弄方差分析表_医学统计与R语言: qvalue
  3. _捷豹F-pace汽车音响改装黄金声学,中道隔音——哈尔滨小蒋
  4. GT Transceiver的总体架构梳理
  5. 初识Tcl(十一):Tcl 命名空间
  6. 【 MATLAB 】any 函数介绍(确定是否有任意数组元素非零)
  7. 【 C 】KR C 与 ANSI C的区别(KR C,ANSI C,C89,C90,C99)(C11)
  8. 周末一起用文本数据库玩玩Code First
  9. 获取远程数据本地缓存到PHP数组
  10. log4j(四)——如何控制不同风格的日志信息的输出?