【问题标题】:how to run chromedriver on aws python如何在aws python上运行chromedriver
【发布时间】:2018-09-08 10:44:05
【问题描述】:

我正在使用 selenium 进行抓取,下面的代码使用本地路径在本地工作。我收到 WebDriverException:消息:未知错误:找不到 Chrome 二进制文件。我不确定我收到此错误是因为我的路径错误还是我必须安装一些东西?我在谷歌上花了好几个小时试图解决这个问题,但没有运气..请帮忙!!

chrome_path = "/home/ec2-user/chrome/chromedriver"
driver = webdriver.Chrome(chrome_path)

book_categories_list = []
for i in asin:
    try:
        url = u"https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dstripbooks&field-keywords="+str(i)
        driver.get(url)
        #time.sleep(1)
        books = driver.find_element_by_class_name('a-expander-container') 
        book_categories_list.append(books.text)
    except:
        book_categories_list.append('Unknown') 

---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-6-436f3e3e0dac> in <module>()
      1 # Set driver and chrome driver path
      2 chrome_path = "/home/ec2-user/chrome/chromedriver"
----> 3 driver = webdriver.Chrome(chrome_path)
      4 
      5 # Url that I will access to scrape

~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options)
     73                 command_executor=ChromeRemoteConnection(
     74                     remote_server_addr=self.service.service_url),
---> 75                 desired_capabilities=desired_capabilities)
     76         except Exception:
     77             self.quit()

~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in __init__(self, command_executor, desired_capabilities, browser_profile, proxy, keep_alive, file_detector, options)
    152             warnings.warn("Please use FirefoxOptions to set browser profile",
    153                           DeprecationWarning)
--> 154         self.start_session(desired_capabilities, browser_profile)
    155         self._switch_to = SwitchTo(self)
    156         self._mobile = Mobile(self)

~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in start_session(self, capabilities, browser_profile)
    241         parameters = {"capabilities": w3c_caps,
    242                       "desiredCapabilities": capabilities}
--> 243         response = self.execute(Command.NEW_SESSION, parameters)
    244         if 'sessionId' not in response:
    245             response = response['value']

~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    310         response = self.command_executor.execute(driver_command, params)
    311         if response:
--> 312             self.error_handler.check_response(response)
    313             response['value'] = self._unwrap_value(
    314                 response.get('value', None))

~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

WebDriverException: Message: unknown error: cannot find Chrome binary
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Linux 4.9.81-35.56.amzn1.x86_64 x86_64)

【问题讨论】:

标签: python-3.x amazon-web-services selenium web-scraping selenium-chromedriver


【解决方案1】:

错误确实给了我们如下提示:

WebDriverException: Message: unknown error: cannot find Chrome binary

这个错误本质上意味着 ChromeDriver 没有在预期的位置找到 Chrome 二进制文件。

可以有以下两种解决方案:

  • 根据RequirementsChrome 安装需要在明确的位置。

  • 您也可以通过Options 类的实例传递Chrome 二进制文件的绝对路径,如下所示:

    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.binary_location = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
    
  • 最后,在启动 WebDriverWebClient 时,您需要发送参数 Key executable_path 以及 值 chrome_path

    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.binary_location = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
    chrome_path = "/home/ec2-user/chrome/chromedriver"
    driver = webdriver.Chrome(executable_path=chrome_path, chrome_options=options)
    

【讨论】:

  • 查看我的答案更新,让我知道状态。
  • 我收到 WebDriverException:消息:未知错误:/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome 错误中没有 chrome 二进制文件
  • @JakePark 检查系统中 chrome 二进制文件的位置,并将 绝对路径 作为值传递给 options.binary_location
  • 现在我收到这个错误我想我要去某个地方 WebDriverException:消息:未知错误:Chrome 无法启动:异常退出
  • @JakePark 好消息!!!因此,您的实际错误 WebDriverException: Message: unknown error: cannot find Chrome binary 已解决。请您对您的新要求提出一个新问题吗?
猜你喜欢
  • 2017-04-24
  • 1970-01-01
  • 1970-01-01
  • 2020-11-27
  • 2017-03-19
  • 2019-07-02
  • 2020-06-27
  • 1970-01-01
相关资源
最近更新 更多