上来先讲重点,如果出现这个问题,先检查两个地方:
一般都是这两个问题造成的。下面是细节:
这两天,我也遇到了这个鬼魅的问题,因为我的自动化程序已经跑了好几个月了,在我没做任何修改的情况下,突然这几天我收不到通知短信了,登上环境一看:
File "/root/app_sign/JD_mobile.py", line 21, in login
driver = create_chrome(disableImage=False, mobile=False)
File "/root/app_sign/common_utils.py", line 42, in create_chrome
driver = Chrome(chrome_options=options)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 156, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 245, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
(Driver info: chromedriver=2.35 (0),platform=Linux 3.10.0-693.2.2.el7.x86_64 x86_64)
这个异常代表着客户端无法链接chromedriver,一般来说,chromedriver在启动之后,会在本地(localhost)上监听9195
端口,not reachable意味着这个端口是无法访问的。
网上很多解决方案是指向了 /etc/hosts
文件中的 127.0.0.1 localhost
。但实际上,我是没有动过这个文件的。
top
排查了一下:
top - 09:17:01 up 68 days, 15:25, 2 users, load average: 4.36, 5.35, 6.08
Tasks: 96 total, 4 running, 91 sleeping, 0 stopped, 1 zombie
%Cpu(s): 46.9 us, 53.1 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1016164 total, 65076 free, 776984 used, 174104 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 38040 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2365 root 20 0 426452 13948 2172 R 97.4 1.4 3808:11 chromedriver
16649 root 20 0 1068404 62032 1288 S 1.0 6.1 526:32.01 python3.6
发现 chromedriver 已经启动了,而且占用了大量的 cpu。
ps -axu | grep chromedriver
继续看进程:
ps -aux | grep chromedriver
root 1864 0.0 0.8 420820 8656 ? Sl Sep03 0:03 chromedriver --port=54253
root 1986 0.0 0.8 420788 8532 ? Sl Sep03 0:04 chromedriver --port=50076
root 2106 0.0 0.8 421844 8492 ? Sl Sep03 0:08 chromedriver --port=60481
root 2224 0.0 0.8 420788 8564 ? Sl Sep03 0:04 chromedriver --port=46809
root 2365 66.1 1.3 426452 13948 ? Sl Sep03 3808:20 chromedriver --port=42449
root 2505 0.0 0.8 419796 8592 ? Sl Sep03 0:08 chromedriver --port=58371
root 4305 0.0 1.0 420788 11060 ? Sl Sep04 0:06 chromedriver --port=48238
把他们都kill掉,然后你会发现,还是启动不了,继续看ps -aux | grep webdriver
:
ps -aux | grep webdriver
root 1872 0.0 3.1 1297316 32364 ? Sl Sep03 0:40 /usr/lib64/chromium-browser/chromium-browser --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-gpu --disable-hang-monitor --disable-images --disable-popup-blocking --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-automation --enable-logging --force-fieldtrials=SiteIsolationExtensions/Control --headless --ignore-certificate-errors --load-extension=/tmp/.org.chromium.Chromium.mjqXiO/internal --log-level=0 --metrics-recording-only --no-first-run --no-sandbox --password-store=basic --remote-debugging-port=12212 --test-type=webdriver --use-mock-keychain --user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" --user-data-dir=/tmp/.org.chromium.Chromium.80hqgb data:,
root 1913 0.0 4.4 1715216 44936 ? Sl Sep03 0:50 /usr/lib64/chromium-browser/chromium-browser --type=renderer --enable-automation --enable-logging --log-level=0 --no-sandbox --test-type=webdriver --use-gl=swiftshader-webgl --disable-gpu-compositing --service-pipe-token=D53136B4A76B7466381D9229384AE7C8 --lang=en-US --headless --user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" --num-raster-threads=1 --service-request-channel-token=D53136B4A76B7466381D9229384AE7C8 --renderer-client-id=4 --shared-files=v8_natives_data:100,v8_snapshot_data:101
root 1994 0.0 3.1 1297348 31960 ? Sl Sep03 0:36 /usr/lib64/chromium-browser/chromium-browser --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-gpu --disable-hang-monitor --disable-images --disable-popup-blocking --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-automation --enable-logging --force-fieldtrials=SiteIsolationExtensions/Control --headless --ignore-certificate-errors --load-extension=/tmp/.org.chromium.Chromium.rQODJb/internal --log-level=0 --metrics-recording-only --no-first-run --no-sandbox --password-store=basic --remote-debugging-port=12134 --test-type=webdriver --use-mock-keychain --user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" --user-data-dir=/tmp/.org.chromium.Chromium.LxceXg data:,
root 2034 0.0 4.3 1714712 44564 ? Sl Sep03 0:39 /usr/lib64/chromium-browser/chromium-browser --type=renderer --enable-automation --enable-logging --log-level=0 --no-sandbox --test-type=webdriver --use-gl=swiftshader-webgl --disable-gpu-compositing --service-pipe-token=9CFBD23CE4B01D39B07357EAC8FF18E3 --lang=en-US --headless --user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" --num-raster-threads=1 --service-request-channel-token=9CFBD23CE4B01D39B07357EAC8FF18E3 --renderer-client-id=4 --shared-files=v8_natives_data:100,v8_snapshot_data:101
只要把之前启动的webdriver都清理完,就能够启动了。
当然,最简单的方法是重启。。。