简体   繁体   中英

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. 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!!

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.

There can be two solutions as follows :

  • As per the Requirements the Chrome installation needs to be in a definite location.

  • As an alternative you can pass the absolute path of the Chrome binary through an instance of Options class as follows :

     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 .

     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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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