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)