selenium
selenium ubuntu
- 系列文章目录
- 前言
- 一、ubuntu
- 二、使用
- 1.chromedriver
- 2.python3
- 3.selenium
- 4.测试
- 参考
前言
selenium ubuntu
一、ubuntu
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb dpkg -i google-chrome-stable_current_amd64.deb
# which google-chrome /usr/bin/google-chrome
# google-chrome --version Google Chrome 102.0.5005.61二、使用 1.chromedriver
# wget https://chromedriver.storage.googleapis.com/102.0.5005.61/chromedriver_linux64.zip # unzip chromedriver_linux64.zip2.python3
# ls -lh /usr/bin/ | grep python lrwxrwxrwx 1 root root 9 Mar 13 2020 python3 -> python3.8 -rwxr-xr-x 1 root root 5.3M Apr 27 2020 python3.8
# pwd /usr/bin # ln -s python3.8 python
# apt install python3-pip -y3.selenium
pip install selenium4.测试
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox') # 这个配置很重要
#client = webdriver.Chrome(chrome_options=chrome_options, executable_path='/usr/bin/chromedriver') # 如果没有把chromedriver加入到PATH中,就需要指明路径
client = webdriver.Chrome(options=chrome_options) # 如果没有把chromedriver加入到PATH中,就需要指明路径
client.get("https://www.baidu.com")
print (client.page_source.encode('utf-8'))
client.quit()
- 测试2
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
#options.binary_location = '/usr/bin/google-chrome'
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(options=options)
url = 'http://www.zhihu.com'
driver.get(url)
#print(driver.page_sources)
print(driver.page_source.encode('utf-8'))
driver.quit()
参考
- 如何在 Ubuntu 20.04 上安装 Google Chrome 网络浏览器
- Ubuntu 配置selenium+chrome headless环境
- Ubuntu 无界面使用selenium chrome + headless



