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)

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM