centos7.4 實例
利用 yum 命令安裝 google-chrome 超級簡單(安裝最新版):
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
chromedriver 也下載最新版就好,和Mac 版本一樣,我依舊選擇這個最新版本:
http://chromedriver.storage.googleapis.com/index.html?path=70.0.3538.16/
Python3.7 之前安裝過:
Centos7 環境下 Python2.7 換成 Python3.7 運行 scrapy 應用所遇到的問題記錄
然后配置一下,就能跑起來 selenium 項目了,當然該 pip install xxx 安裝模塊就缺什么安裝什么了
# 設置 webdriver 參數 options = webdriver.ChromeOptions() # 設置谷歌瀏覽器的一些選項 # proxy 代理 options 選項 options.add_argument(r'--proxy--server=http:\\' + ip) uas = get_uas() # 設置user-agent options.add_argument('user-agent=' + choice(uas)) # 以 headless 方案運行 options.add_argument('--headless') options.add_argument('--no-sandbox') # options.add_argument('--disable-dev-shm-usage') # 禁用圖片訪問 # prefs = {"profile.managed_default_content_settings.images": 2} # options.add_experimental_option("prefs", prefs)
如果不配置 # 以 headless 方案運行
會拋出下面異常
[root@iZwz9117d4ssv2v5oe81m3Z selenium_baidu]# python testRun.py package_util 初始化 testRun.py:32: DeprecationWarning: use options instead of chrome_options driver = webdriver.Chrome(path, chrome_options=options) Exception in thread Thread-1: Traceback (most recent call last): File "/usr/python/lib/python3.7/threading.py", line 917, in _bootstrap_inner self.run() File "/usr/python/lib/python3.7/threading.py", line 865, in run self._target(*self._args, **self._kwargs) File "testRun.py", line 32, in get_url driver = webdriver.Chrome(path, chrome_options=options) File "/usr/python/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__ desired_capabilities=desired_capabilities) File "/usr/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__ self.start_session(capabilities, browser_profile) File "/usr/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "/usr/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/usr/python/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) (Driver info: chromedriver=70.0.3538.16 (16ed95b41bb05e565b11fb66ac33c660b721f778),platform=Linux 3.10.0-693.2.2.el7.x86_64 x86_64)
selenium 代碼正確運行: