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)                       # 获取文本
相关推荐
蒲公英内测分发8 小时前
Typeoff:当 AI 开始参与工作,我们可能需要重新思考“输入”这件事
人工智能·测试工具·项目管理·语音输入·ai语音输入
LT101579744412 小时前
2026年手机机型兼容性测试选型指南:安卓机型碎片化、APP闪退UI错乱如何解决
android·测试工具·ui·智能手机
Luminbox紫创测控15 小时前
GB/T 5137.3 2020标准:汽车安全玻璃耐辐照与耐模拟气候试验方法
人工智能·测试工具·汽车·安全性测试·uv·测试标准
Alan_6911 天前
聊聊 Swagger/Postman/Apifox 的真实选型与 Python 项目实战
python·测试工具·postman
LT10157974441 天前
2026年IE/Chrome/Edge兼容测试选型指南:新旧浏览器内核差异大、后台系统渲染崩溃如何解决
测试工具
icsocket2 天前
芯片测试治具关键组成部分和设计考虑:机械结构中的夹具
测试工具
标致的自行车3 天前
Selenium 爬虫固定开头:
爬虫·selenium·测试工具
安然无虞4 天前
Python自动化测试·Selenium操控元素的方法
python·selenium·测试工具
Data_Journal4 天前
使用 Selenium 进行网页抓取:分步指南
selenium·测试工具·microsoft