centos7.4 64位安装 google-chrome 与 chromedriver 运行 Python selenium 项目
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 代码正确运行: