linux安装selenium步骤

1,安装selenium模块

pip3 install selenium

2,安装谷歌浏览器

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm -y

3
安装chromedriver
1)运行下面命令查看浏览器版本
google-chrome --version

 出现这个代表谷歌浏览器安装成功

2)谷歌浏览器版本最新版124及以后得版本在这里下载

https://googlechromelabs.github.io/chrome-for-testing/#canary

  

 本地下载好解压之后放到服务器任意位置即可,记得加上运行权限

chmod +x chromedriver

最后一步上代码测试

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

# 指定驱动路径
driver_path = '/mnt/chrome/chromedriver'

# 创建ChromeOptions对象
chrome_options = Options()

# 添加Chrome启动参数
chrome_options.add_argument("--headless")   #这两个选项务必加上
chrome_options.add_argument('--no-sandbox')

# 创建Service对象并传入ChromeOptions
service = Service(driver_path)

# 创建WebDriver对象
driver = webdriver.Chrome(service=service, options=chrome_options)

# 打开网页
driver.get('https://www.baidu.com/')

# 输出网页源代码
print(driver.page_source)

# 关闭浏览器
driver.quit()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章