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

相关推荐
laocooon52385788617 分钟前
插入法排序 python
开发语言·python·算法
清水白石0083 小时前
《深入 Python 上下文管理器:contextlib.contextmanager 与类实现方式的底层差异全景解析》
开发语言·python
程序员佳佳3 小时前
GPT-4时代终结?GPT-5.2与Banana Pro实测数据公开,普通开发者如何接住这泼天富贵
开发语言·python·gpt·chatgpt·重构·api·midjourney
Blossom.1183 小时前
多模态大模型LoRA微调实战:从零构建企业级图文检索系统
人工智能·python·深度学习·学习·react.js·django·transformer
小钻风33663 小时前
软件测试: 从入门到实践 (接口测试)
软件测试·python
小鸡吃米…4 小时前
带Python的人工智能——计算机视觉
人工智能·python·计算机视觉
玄同7654 小时前
Python 数据类型:LLM 语料与 API 参数的底层处理逻辑
开发语言·人工智能·python·自然语言处理·llm·nlp·知识图谱
databook4 小时前
数据分析师的“水晶球”:时间序列分析
python·数据挖掘·数据分析
技术路上的探险家5 小时前
vLLM常用启动参数的详细解释
python·大模型·qwen·vllm
WHJ2265 小时前
记录解决jupyter打开闪退
ide·python·jupyter