Ubuntu:安装selenium和chromedriver并测试(不需要图形化界面)

安装chrome驱动

1
2
wget https://npm.taobao.org/mirrors/chromedriver/72.0.3626.7/chromedriver_linux64.zip
unzip chromedriver_linux64.zip

将文件移动到usr/bin文件夹,配置即完成。

1
mv chromedriver /usr/bin/

安装chrome

1
2
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome*.deb

测试代码:

1
2
3
4
5
6
7
8
9
10
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless') # 设置无界面
chrome_options.add_argument('--no-sandbox') # root用户下运行代码需添加这一行
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://www.baidu.com")
print(driver.page_source)
driver.quit()

如果遇到不兼容其他安装包,应该会提示执行命令

1
apt-get install -f

sudo apt-get -f install 是修复损坏的软件包,尝试卸载出错的包,重新安装正确版本。

-------------本文结束 感谢您的阅读-------------