爬虫笔记-selenium的基本使用+元素定位+元素信息

1、selenium的基本使用

首先要在python中的script终端中输入这行命令 pip install selenium

另外需要下载你默认浏览器的驱动器

比如笔者用的是chrome浏览器,所以在关于浏览器中查找自己浏览器的版本号

我的就是96.0.4664.110 ,再从chrome浏览器驱动下载地址:

http://chromedriver.storage.googleapis.com/index.html

1、selenium的基本使用

​​​​
# 导入selenium
from selenium import webdriver
# from selenium.webdriver.chrome.service import Service

# 创建浏览器操作对象
path = 'chromedriver.exe'


browser = webdriver.Chrome(path)

# 访问网站
# url = 'https://www.baidu.com'
url = 'https://www.jd.com/'
browser.get(url)

2、selenium元素定位

from selenium import webdriver

path = 'chromedriver.exe'
browser = webdriver.Chrome(path)

url = 'https://www.baidu.com/'
browser.get(url)

# 元素定位

# 根据id来找到对象
# button = browser.find_element_by_id('su')
# print(button)


# 根据标签属性的属性值来获取对象
# button = browser.find_element_by_name('wd')
# print(button)


# 根据xpath语句来获取对象
# button = browser.find_element_by_xpath('//input[@id="su"]')
# print(button)


# 根据标签的名字来获取对象
# button = browser.find_element_by_tag_name('input')
# print(button)


# 使用bs4的语法来获取对象
# button = browser.find_element_by_css_selector('#su')
# print(button)

3、 selenium元素信息


from selenium import webdriver

path = 'chromedriver.exe'
browser = webdriver.Chrome(path)

url = 'http://www.baidu.com'
browser.get(url)

input = browser.find_element_by_id('su')

# 获取标签的属性
print(input.get_attribute('class'))
# 获取标签的名字
print(input.tag_name)
# 获取元素的文本
a = browser.find_element_by_link_text('新闻')
print(a.text)

http://chromedriver.storage.googleapis.com/index.htmlhttp://chromedriver.storage.googleapis.com/index.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少你三行代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值