简体   繁体   English

如何在AWS Python上运行chromedriver

[英]how to run chromedriver on aws python

I am using selenium to scrape and below code works locally with a local path. 我正在使用硒进行刮擦,以下代码在本地使用本地路径工作。 I'm getting WebDriverException: Message: unknown error: cannot find Chrome binary. 我收到WebDriverException:消息:未知错误:找不到Chrome二进制文件。 I am not sure I'm getting this error because my path is wrong or I have to install something? 我不确定是否收到此错误,因为我的路径错误或必须安装某些东西? I been on google for many hours trying to figure this out but no luck.. please help!! 我在Google上待了好几个小时,试图弄清楚这一点,但是没有运气。

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)

The error does gives us the hint as follows : 该错误确实给了我们如下提示:

WebDriverException: Message: unknown error: cannot find Chrome binary

The error essentially means ChromeDriver didn't find the Chrome binary in the expected location. 该错误实质上意味着ChromeDriver在预期的位置找不到Chrome二进制文件。

There can be two solutions as follows : 可以有以下两种解决方案:

  • As per the Requirements the Chrome installation needs to be in a definite location. 根据要求Chrome的安装位置必须确定。

  • As an alternative you can pass the absolute path of the Chrome binary through an instance of Options class as follows : 或者,您可以通过Options类的实例传递Chrome二进制文件的绝对路径 ,如下所示:

     from selenium.webdriver.chrome.options import Options options = Options() options.binary_location = "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome" 
  • Finally while initiating the WebDriver and WebClient you need to send the argument Key executable_path along with the Value chrome_path . 最后,同时启动的webdriverWeb客户端 ,你需要与价值 chrome_path一起发送的说法主要 executable_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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM