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

推荐订阅源

P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Microsoft Security Blog
Microsoft Security Blog
云风的 BLOG
云风的 BLOG
MongoDB | Blog
MongoDB | Blog
Know Your Adversary
Know Your Adversary
K
Kaspersky official blog
人人都是产品经理
人人都是产品经理
IT之家
IT之家
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
D
DataBreaches.Net
C
CXSECURITY Database RSS Feed - CXSecurity.com
U
Unit 42
博客园 - 【当耐特】
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
H
Help Net Security
L
LangChain Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
Scott Helme
Scott Helme
The GitHub Blog
The GitHub Blog
博客园_首页
P
Proofpoint News Feed
大猫的无限游戏
大猫的无限游戏
S
Secure Thoughts
The Register - Security
The Register - Security
Engineering at Meta
Engineering at Meta
T
Threatpost
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Blog — PlanetScale
Blog — PlanetScale
C
Cybersecurity and Infrastructure Security Agency CISA
T
Troy Hunt's Blog
罗磊的独立博客
腾讯CDC
Simon Willison's Weblog
Simon Willison's Weblog
Security Latest
Security Latest
Last Week in AI
Last Week in AI
P
Privacy & Cybersecurity Law Blog
Latest news
Latest news
The Cloudflare Blog
Jina AI
Jina AI
小众软件
小众软件
Cloudbric
Cloudbric
TaoSecurity Blog
TaoSecurity Blog
F
Full Disclosure
V
V2EX

博客园 - 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个人学习笔记8-获取信息和勾选框 python+selenium个人学习笔记6-元素等待 python+selenium个人学习笔记5-多窗口和多表单切换 python+selenium个人学习笔记4-鼠标和键盘操作
python+selenium个人学习笔记7-警告框处理和下拉框选择
Chen.HJ · 2018-03-07 · via 博客园 - Chen.HJ

警告框处理和下拉框选择

一、警告框处理

PS:三种警告框alert/confirm/prompt(具体样式可以百度。。。)

1、text:获取文本值

driver.switch_to.alert.text

2、accept():接受警告框

driver.switch_to.alert.accept()

3、dismiss():关闭(取消)警告框

driver.switch_to.alert.dismiss()

4、send_keys():发送文本到警告框

driver.switch_to.alert.send_keys()

示例:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('http://www.baidu.com')
link = driver.find_element_by_link_text('设置')
ActionChains(driver).move_to_element(link).perform()# 鼠标悬停至“设置”链接
driver.find_element_by_link_text("搜索设置").click()# 打开搜索设置
sleep(2)
driver.find_element_by_class_name("prefpanelgo").click()# 保存设置
sleep(2)
#获取警告框的文本信息
t=driver.switch_to.alert.text
print(t)
#接受警告框
driver.switch_to.alert.accept()
#关闭(取消)警告框
# driver.switch_to.alert.dismiss()
#发送文本到警告框
# driver.switch_to.alert.send_keys()#示例中无效
driver.quit()

二、下拉框选择

1、导入Select方法

from selenium.webdriver.support.select import Select  

2、通过索引定位

select_by_index()  :通过索引定位

3、通过value值定位

select_by_value()  :通过value值定位

4、通过文本值定位

select_by_visible_text() :通过文本值定位

示例:

from selenium import webdriver
from selenium.webdriver.support.select import Select
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)
# 搜索结果显示条数
sou = driver.find_element_by_xpath("//select[@id='nr']")
Select(sou).select_by_index(1)#显示20条
sleep(2)
Select(sou).select_by_value("50")#显示50条
sleep(2)
Select(sou).select_by_visible_text("每页显示20条")#显示20条
sleep(2)
driver.quit()

Select的其他功能:

deselect_all() :取消所有选项

deselect_by_index() :取消对应index选项

deselect_by_value() :取消对应value选项

deselect_by_visible_text() :取消对应文本选项

first_selected_option():返回第一个选项

all_selected_options():返回所有的选项