Selenium WebDriverException:无法访问 chrome

Selenium WebDriverException: chrome not reachable

提问人:Einreinfall 提问时间:4/26/2020 更新时间:4/26/2020 访问量:1328

问:

我正在尝试使用 Python 抓取 Javascript 页面。看到 PhantomJS 已被弃用,我正在尝试使用无头 Chrome 网络驱动程序加载页面,但无济于事。

驱动程序已加载,但代码大多在等待期间或检索页面源时崩溃。

法典:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

from webdriver_manager.chrome import ChromeDriverManager

def fetch(search):
    BASE_URL = 'https://ko.dict.naver.com/#/search?query='
    DELAY = 10
    html = None

    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)

    search_url = BASE_URL + search
    driver.get(search_url)

    try:
        WebDriverWait(driver, DELAY).until(EC.presence_of_element_located((By.ID, 'content')))
    except TimeoutException:
        print('Took too long to load.')
    else:
        html = driver.page_source
    finally:
        driver.quit()

    return html

堆栈跟踪:

Traceback (most recent call last):
  File "fetch.py", line 44, in <module>
    test = fetch(s)
  File "fetch.py", line 31, in fetch
    WebDriverWait(driver, DELAY).until(EC.presence_of_element_located((By.ID, 'content')))
  File "/home/jrmanrique/.virtualenvs/nltk-kor/lib/python3.6/site-packages/selenium/webdriver/support/wait.py", line 71, in until
    value = method(self._driver)
  File "/home/jrmanrique/.virtualenvs/nltk-kor/lib/python3.6/site-packages/selenium/webdriver/support/expected_conditions.py", line 64, in __call__
    return _find_element(driver, self.locator)
  File "/home/jrmanrique/.virtualenvs/nltk-kor/lib/python3.6/site-packages/selenium/webdriver/support/expected_conditions.py", line 415, in _find_element
    raise e
  File "/home/jrmanrique/.virtualenvs/nltk-kor/lib/python3.6/site-packages/selenium/webdriver/support/expected_conditions.py", line 411, in _find_element
    return driver.find_element(*by)
  File "/home/jrmanrique/.virtualenvs/nltk-kor/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
    'value': value})['value']
  File "/home/jrmanrique/.virtualenvs/nltk-kor/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/jrmanrique/.virtualenvs/nltk-kor/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
  (Session info: headless chrome=81.0.4044.122)

系统信息:

Ubuntu 18.04 on Windows
Google Chrome 81.0.4044.122
Chromedriver 81.0.4044.69
Selenium 3.141.0
python selenium selenium-webdriver chrome-web-driver

评论

0赞 ashutosh singh 4/26/2020
您可以运行以下命令,然后尝试运行您的代码吗? 还要确保你把wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'apt-get -y updateapt-get install -y google-chrome-stableln -s /usr/bin/chromium-browser && chmod 777 /usr/bin/chromium-browserchromedriver/usr/local/bin
0赞 ashutosh singh 4/26/2020
将空格视为命令的结尾

答: 暂无答案