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)                       # 获取文本
相关推荐
万粉变现经纪人2 小时前
如何解决 pip install 安装报错 ModuleNotFoundError: No module named ‘tokenizers’ 问题
python·selenium·测试工具·scrapy·beautifulsoup·fastapi·pip
我的xiaodoujiao4 小时前
Windows系统Web UI自动化测试学习系列2--环境搭建--Python-PyCharm-Selenium
开发语言·python·测试工具
RE-19018 小时前
制冷剂中表压对应温度值的获取(Selenium)
爬虫·python·selenium·jupyter·pandas·danfoss·reftools
测试老哥8 小时前
Python+selenium自动化生成测试报告
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
ThreeAu.8 小时前
2025年Web自动化测试与Selenium面试题收集:从基础到进阶的全方位解析
自动化测试·软件测试·selenium·测试工具·面试·web测试·测试开发工程师
测试199812 小时前
Web自动化测试之测试用例流程设计
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
Heliotrope_Sun15 小时前
Postman使用指南
测试工具·postman
程序员三藏1 天前
Fiddler抓取HTTPS
自动化测试·软件测试·python·测试工具·https·fiddler·接口测试
gc_22991 天前
学习Python中Selenium模块的基本用法(15:窗口操作)
python·selenium