centos7上PhantomJS 過時以後改用Chrome時填的坑

忽然有個自動化需求因此準備使用模擬點擊的方法,html

在使用以前的PhantomJS時,報錯python

UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome ……web

這時候的方法就是換到chrome了,若是你沒裝過,那麼能夠按照這裏的方法去安裝,若是已經安裝了,也建議按照這裏的方法去排查可能出現的問題,chrome

For Linuxapi

  1. Check you have installed latest version of chrome brwoser-> chromium-browser -version
  2. If not, install latest version of chrome sudo apt-get install chromium-browser
  3. get appropriate version of chrome driver from here
  4. Unzip the chromedriver.zip
  5. Move the file to /usr/bin directory sudo mv chromedriver /usr/bin
  6. Goto /usr/bin directory cd /usr/bin
  7. Now, you would need to run something like sudo chmod a+x chromedriver to mark it executable.
  8. finally you can execute the code.app

    from selenium import webdriver driver = webdriver.Chrome() driver.get("http://www.google.com") print driver.page_source.encode('utf-8') driver.quit() display.stop()

這樣只會就應該能夠用了,less

若是還報錯,selenium.common.exceptions.WebDriverException: Message: '' executable may have wrong permissions.測試

那極有多是你沒把執行路徑添加到path,或者添加的路徑不完整,全路徑應該是包含chromedriver這個文件名的,不能只寫到它所在的文件夾,ui

eg,google

chrome_path = '/usr/bin/chromedriver'
這樣就能夠了,下面是測試代碼,正常執行的,
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-extensions')

chrome_path = '/usr/bin/chromedriver'
driver = webdriver.Chrome(executable_path=chrome_path, chrome_options=chrome_options)

driver.get("https://cnblogs.com/")
相關文章
相關標籤/搜索