惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
人人都是产品经理
人人都是产品经理
博客园 - 司徒正美
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
量子位
罗磊的独立博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
小众软件
小众软件
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
大猫的无限游戏
大猫的无限游戏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园_首页
博客园 - 叶小钗
V
Vulnerabilities – Threatpost
T
The Exploit Database - CXSecurity.com
T
Tailwind CSS Blog
IT之家
IT之家
博客园 - 聂微东
Spread Privacy
Spread Privacy
V2EX - 技术
V2EX - 技术
S
Security Affairs
宝玉的分享
宝玉的分享
V
V2EX
C
Cisco Blogs
博客园 - Franky
美团技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
S
Securelist
J
Java Code Geeks
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
WordPress大学
WordPress大学
W
WeLiveSecurity
T
Threatpost
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志

博客园 - Chen.HJ

python用正则对字符串进行运算 Nginx安装和遇到过的坑 Redis安装与常用命令 Nginx、Tomcat和Redis配置文件说明 python + selenium 获取标签文本的为空解决办法 Python读取Excel Python使用paramiko的SFTP get或put整个目录 Linux系统性能监控工具nmon python+selenium个人学习笔记11-登录封装与调用 python+selenium+unittest测试框架4-邮件发送最新测试报告 python+selenium+unittest测试框架3-项目构建和发送邮件 python+selenium+unittest测试框架2-装饰器@classmethod python+selenium+unittest测试框架1-unittest单元测试框架和断言 python+selenium个人学习笔记10-调用JavaScript和截图 python+selenium个人学习笔记9-文件上传和cookie操作 python+selenium个人学习笔记7-警告框处理和下拉框选择 python+selenium个人学习笔记6-元素等待 python+selenium个人学习笔记5-多窗口和多表单切换 python+selenium个人学习笔记4-鼠标和键盘操作
python+selenium个人学习笔记8-获取信息和勾选框
Chen.HJ · 2018-03-08 · via 博客园 - Chen.HJ

获取信息、定位一组元素和勾选框

一、获取信息

在实际操作中,我们通常通过获取title 、URL和text等信息进行断言。

1、获取当前页面的title

2、获取当前页面的URL

3、获取搜索元素的文本信息

driver.find_element_by_class_name('nums').text

示例:

from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
#打印当前页面title
title1 = driver.title
print(title1)
#打印当前页面URL
url1 = driver.current_url
print(url1)
driver.find_element_by_id("kw").send_keys("chen")
driver.find_element_by_id("su").click()
sleep(1)
#打印当前页面title
title2 = driver.title
print(title2)
#打印当前页面URL
url2 = driver.current_url
print(url2)
#获取元素对应的文本信息
user = driver.find_element_by_xpath(".//*[@id='container']/div[2]/div/div[2]").text
print(user)
driver.quit()

二、定位一组元素

定位一组元素和定位单个元素方法类似,总共八种方法,如下:

find_elements_by_id()

find_elements_by_name()

find_elements_by_class_name()

find_elements_by_tag_name()

find_elements_by_link_text()

find_elements_by_partial_link_text()

find_elements_by_xpath()

find_elements_by_css_selector()

示例:

from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.find_element_by_id("kw").send_keys("chen")
driver.find_element_by_id("su").click()
sleep(1)
# 定位一组元素
ttext = driver.find_elements_by_xpath('//h3/a')
# 循环遍历出每一条搜索结果的标题
for t in ttext:
    print(t.text)
driver.quit()

三、勾选框

1、单选框

可直接通过元素定位进行选择。

driver.find_element_by_xpath("xxx").click()

2、复选框

先定位元素组,然后通过for循环去进行点击操作

double = driver.find_element_by_xpath("xxx")
for i in double:
    i.click()

3、判断选框状态

driver.find_element_by_id("xxx").is_selected()

示例:

from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("http://www.baidu.com")
driver.find_element_by_link_text("设置").click()
sleep(2)
driver.find_element_by_link_text("高级搜索").click()
sleep(2)
#勾选前,判断选框状态
asser = driver.find_element_by_xpath(".//*[@id='q5_1']").is_selected()
print(asser)
#直接点击选框
driver.find_element_by_xpath(".//*[@id='q5_1']").click()
sleep(2)
#勾选后,判断选框状态
asser1 = driver.find_element_by_xpath(".//*[@id='q5_1']").is_selected()
print(asser1)
driver.quit()