1、Select元素

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

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

3.选项有三个

每页显示 10 条

每页显示 20 条

每页显示 50 条

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 seleniumimport webdriver

from selenium.webdriver.common.action_chainsimport 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()

s= 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 seleniumimport webdriver

from selenium.webdriver.common.action_chainsimport 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 seleniumimport webdriver

from selenium.webdriver.common.action_chainsimport ActionChains

from selenium.webdriver.support.selectimport 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()

s= driver.find_element_by_id("nr")

Select(s).select_by_index(2)

2.4 Select 模块(value)定位

1.Select 模块里面除了 index 的方法,还有一个方法,通过选项的 value值来定位。每个选项,都有对应的 value 值,如

每页显示 10 条

每页显示 20 条

每页显示 50 条

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 seleniumimport webdriver

from selenium.webdriver.common.action_chainsimport ActionChains

from selenium.webdriver.support.selectimport 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()

s= 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 seleniumimport webdriver

from selenium.webdriver.common.action_chainsimport ActionChains

from selenium.webdriver.support.selectimport 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定位

s= 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() :返回所有的选项

python实时定位_selenium python 一些操作和定位收集相关推荐

  1. absolute如果找不到定位父元素那么会相对于谁进行定位_selenium+python面试题目总结,完整度80%,看看你会多少?...

    很多小伙伴会经常私信来问我问题,有些来不及回答,实在抱歉! 本篇有点长!看到最后,给自己一个学习的地方! 1. WebDriver原理 webDriver是按照client/server模式设计,cl ...

  2. selenium python怎么读_selenium+Python中的面试总结

    1. WebDriver原理 webDriver是按照client/server模式设计,client就是我们的测试代码,发送请求,server就是打开的浏览器来打开client发出的请求并做出响应. ...

  3. selenium python文档_selenium+python实现百度文库word文档抓取

    更新这个代码最近又改了一点,另外和其他一些小程序一起放在了一个jupyter notebook里,现在挂在github上面.github现在对jupyter notebook的支持非常的好,甚至可以直 ...

  4. pythonxpath定位_selenium+python自动化-xpath定位语法

    前言 右键查看目标元素的xpath地址,这样查看比较死板,不够灵活,有时候直接复制粘贴会定位不到.这个时候就需要自己手动的去写xpath了,这一篇详细讲解xpath的一些语法. 什么是xpath呢? ...

  5. python selenium 下拉列表_Selenium+Python之下拉菜单的定位

    https://www.cnblogs.com/desperado0807/p/4839502.html 1.通过selenium.webdriver.support.ui的Select进行定位 下拉 ...

  6. python web自动化_Selenium+Python Web自动化

    Selenium官方网站 http://selenium-python.readthedocs.io/ 配置使用环境 下载相应的浏览器驱动, Firefox 是默认的 本文以 chrome 为主 ,放 ...

  7. python自动评论_selenium+python 的微博自动转赞评功能实现

    放假了,在家制作了一个selenium+python的微博自动转赞评程序. 程序分为四部分,依次是:登录+点赞+评论+转发,当点赞达到用户上限时去评论,评论至上限时去转发,出现验证码则退出程序.演示视 ...

  8. python鼠标碰撞_selenium + python 鼠标事件

    十.鼠标事件 本章重点: ActionChains 类  context_click() 右击  double_click() 双击  drag_and_drop() 拖动 测试的产品中有一个操 ...

  9. python 滚动条方法_selenium+python 自动化中界面滚动条操作方法

    虽然webdriver提供了操作浏览器的前进和后退的方法,但对于浏览器滚动条并没有提供相应的操作方法,以下使用的方法: 借助JavaScript来控制浏览器的滚动条,webdriver提供了execu ...

最新文章

  1. c语言程序的多文件组织,C代码多文件的组织
  2. 【微信小程序】——实战开发之和风(含demo)
  3. leetcode669. 修剪二叉搜索树
  4. javafx动画_JavaFX动画工具
  5. JZOJ5857 【NOIP提高组模拟A组2018.9.8】没有上司的舞会
  6. linux 串口总线,linux中的serio(虚拟串行输入输出)总线
  7. [WCF]相关资料整理
  8. 不重复计数函数php,EXCEL多条件不重复计数函数是什么
  9. 非常详细的机器学习知识点汇总(二)之SVM23问
  10. 第六次团队作业+登录界面
  11. dj打碟怎么学_关于DJ入门以及手把手教你如何打碟(转)
  12. mongoVUE的增删改查操作使用说明
  13. 学校计算机管理员安全责任书,实验室管理员安全责任书
  14. 塑胶产品内滑块设计要点,以及注意哪些事项?
  15. python神经网络图像分类,图像分类卷积神经网络
  16. html优秀作品展示,31个漂亮的作品展示网页设计
  17. pdf怎么压缩文件到最小?pdf文件怎么变小内存?
  18. AES加解密流程及方法
  19. 帧定格(用于定格画面添加字幕或者图片)
  20. 我的电脑中多了CD驱动器怎么办

热门文章

  1. Linux监控平台搭建( zabbix监控)
  2. 恒生电子发布云计算金融应用“超云计划”
  3. 编写一个函数itob(int n,char s[], int b),将整数n转换为以b进制的数。保存到s中。...
  4. 逻辑地址,线性地址,物理地址
  5. linux,mac,unix 系统下cd 进入有空格的目录或者打开有空格的文件
  6. [Prism]Composite Application Guidance for WPF(10)——系列目录导航
  7. [J2ME QA]真机报告MontyThread -n的错误之解释
  8. manjaro linux换源
  9. 解决argo workflow报错:MountVolume.SetUp failed for volume “docker-sock“ : hostPath type check failed
  10. K8S创建role命令示例