记一次ubuntu下selenium环境的搭建。
windows环境顺畅。
windows下下载好对应的chromedriver 放到指定目录后跑起来很顺畅很成功
http://npm.taobao.org/mirrors/chromedriver/
ubuntu环境:
安装apt-get install google-chrome-stable
下载chromedriver放在usr/bin/chromedriver
chrome安装目录.png
然而。。。。。报错!!
Traceback (most recent call last):
File "/data/ONLINE/api_spider/app/analysis_html.py", line 265, in <module>
nodes = get_all_loop_nodes(url)
File "/data/ONLINE/api_spider/app/analysis_html.py", line 112, in get_all_loop_nodes
web_url_soup = get_url_soup(url)
File "/data/ONLINE/api_spider/app/analysis_html.py", line 143, in get_url_soup
driver = webdriver.Chrome(options=options)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-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-stable is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
报错嘛简单,那就baidu,google。然后找遍百度翻遍google90%的解决方案都是:
github上的解决方法.png
参照这个方法在代码中添加options后依然报错,无论这么google修改都报错,然后放弃了。
过了一周那就尝试自己分析异常信息:The process started from chrome location /usr/bin/google-chrome-stable is no longer running
意思是chrome启动失败嘛,那就先解决chrome启动失败问题。
启动chrome失败加上--no-sandbox也失败:
终端运行chrome.png
然后又找 "chrome linux启动失败"解决方法:
vi /usr/bin/google-chrome
将 exec -a "$0" "$HERE/chrome" "$@" 改为
exec -a "$0" "$HERE/chrome" "$@" --user-data-dir --no-sandbox
参照修改后依然失败(有些地方说需要重启系统,没有试过)
但是!!! 这时候运行我的python代码错误变了错误变成了:
image.png
而且是运行近1分钟后抛出的错误。虽然没有运行成功,但是至少打印错误变了就是好消息。运行1分钟才抛出异常,那这分钟再干嘛!带着这个问题 运行py程序时看看有没有chrome相关进程在搞事情:ps -ef|grep chrome,一看进程果然一大堆chrome相关进行处于运行状态,那"chrome linux启动失败"这个问题应该是解决了。
接下来就是解决“DevToolsActivePort file doesn't exist”问题,
options.add_argument("--remote-debugging-port=9222") #解决DevToolsActivePort file doesn't exist问题
非常奇怪--remote-debugging-port=9222只是指定chrome debug端口而已怎么会解决DevToolsActivePort file doesn't exist呢。。。
更诡异的是最后我把后面“chrome linux启动失败”和“DevToolsActivePort file doesn't exist”的解决方法回退,代码也能正常运行了!!
猜测:chrome新装环境问题,装好后重启系统也许就正常了
网友评论