selenium+python

selenium

八大查找元素

python 复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By

# 创建一个 WebDriver 实例
driver = webdriver.Chrome()

# 打开网页
driver.get("https://www.baidu.com/")

# 使用 find_element 方法查找元素
element = driver.find_element(By.ID, "kw")

element.click()
element.send_keys("")
element.clear()
python 复制代码
element = driver.find_element(By.NAME, "wd")

element = driver.find_element(By.CLASS_NAME, "s_ipt")

element = driver.find_element(By.TAG_NAME, "tag_name")

element = driver.find_element(By.LINK_TEXT, "新闻")

element = driver.find_element(By.PARTIAL_LINK_TEXT, "新")

element = driver.find_element(By.CSS_SELECTOR, "css_selector")

element = driver.find_element(By.XPATH, "//*[@id='kw']")

driver.find_elements()用法

获取标签属性名的值

python 复制代码
element = driver.find_element(By.NAME, "wd")
element.get_attribute("class")
# 获取文本信息
driver.find_element(By.LINK_TEXT,"新闻").text

进入frame框架

python 复制代码
# 比如<frame name="haha">
driver.switch_to.frame("haha")

出框架

python 复制代码
driver.switch_to.default_content()

多选框

python 复制代码
sel = Select(driver.find_element(By.NAME,"select_name"))
# 假如<option value="1"></option>
sel.select_by_value("1")

# <option value="1">哈哈</option>
sel.select_by_visible_text("哈哈")

sel.select_by_index(3)

弹窗

python 复制代码
# alert(只有确认),confirm(有确认有取消),prompt(有确认有取消,可以输入值)
ale = driver.switch_to.alert
# 确认弹窗
ale.accept()
# 取消弹窗
ale.dismiss()
# 获取弹窗文本
strs = ale.text

文件上传

python 复制代码
driver.find_element(By.XPATH,"").send_keys(r"D://")

DDT

相关推荐
闵孚龙2 小时前
动态图机制:为什么 PyTorch 调试起来更舒服
人工智能·pytorch·python
chushiyunen3 小时前
langchain4j笔记、tools
笔记·python·flask
程序员三藏3 小时前
Web自动化测试详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
在放️4 小时前
Python 爬虫 · 第三方代理接入与合规使用
开发语言·爬虫·python
财经资讯数据_灵砚智能4 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年6月14日
大数据·人工智能·python·ai·信息可视化·自然语言处理·灵砚智能
JOJO数据科学7 小时前
JupyterLab Electron 鸿蒙 PC 适配全记录:从 Python 原生崩溃到 node-static 本地工作台
python·electron·harmonyos
xufengzhu7 小时前
第三方 Python 库 redis-py + hiredis 的使用
开发语言·redis·python
深蓝电商API7 小时前
Selenium 5.0 全新架构解析:值得升级吗?
爬虫·selenium
llxxyy卢7 小时前
polar夏季赛部分题目
开发语言·python
闵孚龙7 小时前
PyTorch 系列 之 nn.Module:所有模型的骨架
人工智能·pytorch·python