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)
相关推荐
dreams_dream1 天前
Flask
后端·python·flask
mywpython1 天前
用Python和Websockets库构建一个高性能、低延迟的实时消息推送服务
python·websocket
ZPC82101 天前
FPGA 部署ONNX
人工智能·python·算法·机器人
一晌小贪欢1 天前
Python键盘鼠标自动化库详解:从入门到精通
python·自动化·计算机外设·python鼠标·python键盘·python操控鼠标·python操控键盘
穿西装的水獭1 天前
python将Excel数据写进图片中
开发语言·python·excel
xiaoxiongip6661 天前
假设两个设备在不同网段,网关怎么设置才能通呢
网络·爬虫·python·https·智能路由器
逻极1 天前
Scikit-learn 实战:15 分钟构建生产级中国房价预测模型
python·机器学习·scikit-learn
行板Andante1 天前
AttributeError: ‘super‘ object has no attribute ‘sklearn_tags‘解决
人工智能·python·sklearn
tryCbest1 天前
Python基础之爬虫技术(一)
开发语言·爬虫·python
husterlichf1 天前
逻辑回归以及python(sklearn)详解
python·逻辑回归·sklearn