Selenium元素的常用操作方法分析
作者:喷跑的豆子 时间:2021-09-21 14:51:54
本文实例讲述了Selenium元素的常用操作方法。分享给大家供大家参考,具体如下:
Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成 .Net、Java、Perl等不同语言的测试脚本。
在前面一篇《Selenium定位元素操作》中已经介绍了如何对元素进行定位。定位只是第一步,定位之后需要对这个元素进行操作,或单击(按钮)或输入(文本框),下面就记录一下Selenium中元素的最常用的几个方法:
点击和输入
clear()
:清除文本send_keys(value)
:按键模拟输入click()
:单击元素
from selenium.webdriver import Chrome()
driver = Chrome('C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe')
driver.get('https://www.baidu.com/')
driver.find_element_by_id('kw').clear()
driver.find_element_by_id('kw').send_keys('python')
driver.find_element_by_id('su').click()
提交
submit()
submit()
方法用于提交表单,例如,在输入框输入关键字之后的回车操作,就可以通过该方法模拟:
from selenium.webdriver import Chrome
driver = Chrome('C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe')
driver.get('https://www.baidu.com/')
element = driver.find_element_by_id('kw')
element.send_keys('python')
element.submit()
有时候submit()
可以和click()
方法互换来使用,submit()
同样可以提交一个按钮,但submit()
的应用范围远远没有click()
广
其他常用方法
size
:返回元素的尺寸text
:返回元素的内容get_attribute(name)
:返回元素属性的值is_displayed()
:设置该元素是否用户可见
from selenium.webdriver import Chrome()
driver = Chrome('C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe')
driver.get('https://www.baidu.com/')
# 获取元素的尺寸
element = driver.find_element_by_id('su')
print(element.size) # {'height': 36, 'width': 100}
# 获取元素的内容
element = driver.find_element_by_id('setf')
print(element.text) # 把百度设为主页
# 获取元素中value属性的值
element = driver.find_element_by_id('su')
print(element.get_attribute('value') # 百度一下
# 获取该元素是否用户可见
element = driver.find_element_by_id('su')
print(element.is_displayed()) # True
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/y472360651/article/details/79668160
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解读HTML:大厦的基石
python中的turtle库函数简单使用教程
![](https://img.aspxhome.com/file/2023/4/92054_0s.jpg)
python实现图像随机裁剪的示例代码
![](https://img.aspxhome.com/file/2023/9/118329_0s.png)
appium测试之APP元素定位及基本工具介绍
![](https://img.aspxhome.com/file/2023/6/104986_0s.png)
如何控制弹出一个NTLM验证窗口?
![](https://img.aspxhome.com/file/UploadPic/200912/16/ntlm-36s.gif)
mysql proxy问题
php函数serialize()与unserialize()用法实例
Numpy中的数组搜索中np.where方法详细介绍
Python使用crontab模块设置和清除定时任务操作详解
![](https://img.aspxhome.com/file/2023/6/80776_0s.png)
tensorflow识别自己手写数字
![](https://img.aspxhome.com/file/2023/1/99431_0s.jpg)
python+selenium实现京东自动登录及秒杀功能
Python中namedtuple 命名元祖的使用
如何实现SQL Server 2005快速Web分页
Python UnicodedecodeError编码问题解决方法汇总
![](https://img.aspxhome.com/file/2023/2/67392_0s.png)
Oracle CBO 的 _sort_elimination_cost_ratio 参数
pytorch读取图像数据转成opencv格式实例
python实现根据给定坐标点生成多边形mask的例子
Python word文本自动化操作实现方法解析
![](https://img.aspxhome.com/file/2023/1/121071_0s.jpg)
golang中的单引号转义问题
Python3.10耙梳加密算法Encryption种类及开发场景
![](https://img.aspxhome.com/file/2023/7/89167_0s.png)