selenium 输入框、按钮,输入点击,获取元素属性等简单例子

元素操作

nput框 输入send_keys

input框 清除clear()

按钮 点击click()

按钮 提交submit()

获取元素

tag_name、

class属性值、

坐标尺寸

python 复制代码
"""
input框 输入1次,再追加输入一次, 清除, 再重新输入, 点击百度按钮,或提交
"""
from selenium import webdriver
import time


# 创建浏览器驱动对象
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()            # 参数写浏览器驱动文件的路径,若配置到环境变量就不用写了
# 访问网址
driver.get("https://www.baidu.com/")


inputele = driver.find_element(By.ID,'kw')
inputele.send_keys("嫦娥")
time.sleep(1)
inputele.send_keys("5号")        # 表示它是追加写入

time.sleep(1)

inputele.clear()
inputele.send_keys("嫦娥6号")


time.sleep(0.5)
submitele = driver.find_element(By.ID,'su')
# submitele.submit()            # 提交
submitele.click()               # 点击
python 复制代码
"""
获取 页面标题 url

获取输入框的, tag_name、class属性值、坐标尺寸

driver.title
driver.current_url

----------------元素的
.tag_name				# 该元素的标签名
.text				# 文本
.get_attribute("class")	 	# 元素的某个属性值
.location   .size			# 元素的坐标尺寸

"""
from selenium import webdriver
import time


# 创建浏览器驱动对象
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()            # 参数写浏览器驱动文件的路径,若配置到环境变量就不用写了
# 访问网址
driver.get("https://www.baidu.com/")


# ------获取标题路径------
print(driver.title)
print(driver.current_url)
# ------获取标题路径------

inputele = driver.find_element(By.ID,'kw')
print(inputele.tag_name)                    # 获取(元素)标签名
print(inputele.get_attribute("class"))      # 获取(元素)某个属性
print(inputele.location)                    # 获取(元素)坐标 ,原点在屏幕左上角
print(inputele.size)                        # 获取(元素)尺寸

submitele = driver.find_element(By.CSS_SELECTOR,'[class="mnav c-font-normal c-color-t"]')
print(submitele.text)                       # 获取文本
相关推荐
llilian_168 小时前
信号发生器 多通道多功能脉冲信号发生器应用解决方案 多功能脉冲发生器
功能测试·单片机·嵌入式硬件·测试工具
程序员小远9 小时前
使用Postman进行一次完整的接口测试
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
我的xiaodoujiao10 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 44--将自动化测试结果自动推送至钉钉工作群聊
前端·python·测试工具·ui·pytest
生活很暖很治愈10 小时前
GUI自动化测试[3]——控件&数鼠标操作
windows·python·功能测试·测试工具
我的xiaodoujiao12 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 45--生成项目需要的requirements.txt依赖文件
python·学习·测试工具·pytest
uyeonashi14 小时前
Boost搜索引擎测试报告
功能测试·selenium·搜索引擎
@zulnger16 小时前
selenium 自动化测试工具实战项目(订单)
selenium·测试工具·自动化
深圳博众测控1 天前
博众测控 | ISO 16750-2:2023汽车电气测试新标准解读:关键变化与测试设备选型
人工智能·测试工具·汽车
测试界的世清1 天前
金九银十软件测试面试题(800道)
测试工具·面试·职场和发展
帝落若烟1 天前
loadrunner {“msg“:“请求访问:/getInfo,认证失败,无法访问系统资源“,“code“:401}
测试工具·压力测试