Python爬虫——selenium_元素定位

元素定位:自动化要做的就是模拟鼠标和键盘来操作这些元素,点击,输入等等。操作这些元素前首先要找到它们,WebDriver提供很多定位元素的方法

python 复制代码
from selenium import webdriver

# 创建浏览器对象
path = 'files/chromedriver.exe'
browser = webdriver.Chrome(path)

# 访问网址
url = 'https://www.baidu.com'
browser.get(url)
  1. 根据id来找到对象【常用】
python 复制代码
button = browser.find_element_by_id('su')
print(button)
  1. 根据标签属性的属性值来获取对象
python 复制代码
button = browser.find_element_by_name('wd')
print(button)
  1. 根据xpath语句来获取对象【常用】 find_elements_by_xpath可以返回多个数据,返回类型是列表
python 复制代码
button = browser.find_element_by_xpath('//input[@id="su"]')
print(button)
  1. 根据标签名字来获取对象
python 复制代码
button = browser.find_elements_by_tag_name('input')
print(button)
  1. 使用bs4的语法来获取对象【常用】
python 复制代码
button = browser.find_element_by_css_selector('#su')
print(button)
  1. 获取当前页面的链接文本
python 复制代码
button = browser.find_element_by_link_text('新闻')
print(button)
相关推荐
人工智能时代 准备好了吗19 分钟前
AI回答内容进入率监测:引用识别、文本匹配与语义判断
开发语言·人工智能·python
言乐61 小时前
Python实现建造微服务商城后台
开发语言·python·算法·微服务·架构
fenglllle2 小时前
chromadb emmbedding 向量检索
人工智能·python·embedding
cui_ruicheng2 小时前
Python从入门到实战(六):非序列容器
开发语言·python
百里香酚兰2 小时前
【python学习笔记】pyttsx3库疑似只播报一次语音
笔记·python·学习
飞猪~2 小时前
Langchain python版本 LLM 重要函数invoke,ainvoke,stream,astream,batch,abatch
python·langchain·batch
沙蒿同学3 小时前
当古诗词遇上 AI:从 38 万句诗词中取一个好名字
python·算法·架构
xxie1237943 小时前
Python装饰器与语法糖
开发语言·python
CClaris3 小时前
大模型量化从0到1(五):GPTQ 原理详解 + 从零量化一个真实大模型
人工智能·python·算法·机器学习
Railshiqian4 小时前
UserPickerActivity 内部逻辑分析
开发语言·python