記一次ubuntu下selenium環(huán)境的搭建速那。
windows環(huán)境順暢书妻。
windows下下載好對(duì)應(yīng)的chromedriver 放到指定目錄后跑起來很順暢很成功
http://npm.taobao.org/mirrors/chromedriver/
ubuntu環(huán)境:
安裝apt-get install google-chrome-stable
下載chromedriver放在usr/bin/chromedriver
然而诡挂。徙垫。迅办。肴裙。趾唱。報(bào)錯(cuò)!蜻懦!
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.)
報(bào)錯(cuò)嘛簡(jiǎn)單甜癞,那就baidu,google。然后找遍百度翻遍google90%的解決方案都是:
參照這個(gè)方法在代碼中添加options后依然報(bào)錯(cuò)宛乃,無論這么google修改都報(bào)錯(cuò)悠咱,然后放棄了。
過了一周那就嘗試自己分析異常信息:The process started from chrome location /usr/bin/google-chrome-stable is no longer running
意思是chrome啟動(dòng)失敗嘛征炼,那就先解決chrome啟動(dòng)失敗問題析既。
啟動(dòng)chrome失敗加上--no-sandbox也失敗:
然后又找 "chrome linux啟動(dòng)失敗"解決方法:
vi /usr/bin/google-chrome
將 exec -a "$0" "$HERE/chrome" "$@" 改為
exec -a "$0" "$HERE/chrome" "$@" --user-data-dir --no-sandbox
參照修改后依然失斊馐摇(有些地方說需要重啟系統(tǒng)渡贾,沒有試過)
但是!P塾摇空骚! 這時(shí)候運(yùn)行我的python代碼錯(cuò)誤變了錯(cuò)誤變成了:
而且是運(yùn)行近1分鐘后拋出的錯(cuò)誤。雖然沒有運(yùn)行成功擂仍,但是至少打印錯(cuò)誤變了就是好消息囤屹。運(yùn)行1分鐘才拋出異常,那這分鐘再干嘛逢渔!帶著這個(gè)問題 運(yùn)行py程序時(shí)看看有沒有chrome相關(guān)進(jìn)程在搞事情:ps -ef|grep chrome肋坚,一看進(jìn)程果然一大堆chrome相關(guān)進(jìn)行處于運(yùn)行狀態(tài),那"chrome linux啟動(dòng)失敗"這個(gè)問題應(yīng)該是解決了肃廓。
接下來就是解決“DevToolsActivePort file doesn't exist”問題智厌,
options.add_argument("--remote-debugging-port=9222") #解決DevToolsActivePort file doesn't exist問題
非常奇怪--remote-debugging-port=9222只是指定chrome debug端口而已怎么會(huì)解決DevToolsActivePort file doesn't exist呢。盲赊。铣鹏。
更詭異的是最后我把后面“chrome linux啟動(dòng)失敗”和“DevToolsActivePort file doesn't exist”的解決方法回退,代碼也能正常運(yùn)行了0ⅰ诚卸!
猜測(cè):chrome新裝環(huán)境問題葵第,裝好后重啟系統(tǒng)也許就正常了