linux环境无界面运行selenium

以sentos为例

安装chrome

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

安装chromedriver

  https://npm.taobao.org/mirrors/chromedriver/ 2.4.1版本

  并添加到PATH

  ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver

  ln -s /usr/local/share/chromedriver /usr/bin/chromedrive

安装yum install GConf2

代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


chrome_options = Options()
chrome_options.add_argument('--headless') # 
chrome_options.add_argument('disable-dev-shm-usage')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('no-sandbox')
driver = webdriver.Chrome(chrome_options=chrome_options,port=9515)
# driver = phantomjsWebdriver.WebDriver()
driver.get('https://www.baidu.com')
driver.find_element(By.ID, 'kw').send_keys('selenium')
driver.find_element(By.ID, 'su').click()
print(driver.title)
 
原文地址:https://www.cnblogs.com/yaoqingzhuan/p/12595431.html