Search code examples
selenium-webdriverautomationtypeerrorbinaryfiles

Windows Python Selenium chrome binary issue? No chrome binary at C:\Program Files\Google\Chrome\Application\chrome.exe


So I'm fairly new to coding and I am supposed to be parsing Yelp reviews so I can analyze the data using Pandas. I have been trying to use selenium/beautifulsoup to automate the whole process, but I can't get past the chrome binary location errors in each version of the code I make. I feel like I've tried everything, can someone please tell me what I'm doing wrong?

!pip install selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import pandas as pd
import os

# Set the path to the ChromeDriver executable
chromedriver_path = "C:\\Users\\5mxz2\\Downloads\\chromedriver_win32\\chromedriver"

# Set the path to the Chrome binary
chrome_binary_path = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"  # Update this with the correct path to your Chrome binary

# Set the URL of the Yelp page you want to scrape
url = "https://www.yelp.com/biz/gelati-celesti-virginia-beach-2"

# Set the options for Chrome
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")  # Run Chrome in headless mode, comment this line if you want to see the browser window
chrome_options.binary_location = chrome_binary_path

# Create the ChromeDriver service
service = Service(chromedriver_path)

# Create the ChromeDriver instance
driver = webdriver.Chrome(service=service, options=chrome_options)

# Load the Yelp page
driver.get(url)

# Extract the page source and pass it to BeautifulSoup
soup = BeautifulSoup(driver.page_source, "html.parser")

# Find all review elements on the page
reviews = soup.find_all("div", class_="review")

# Create empty lists to store the extracted data
review_texts = []
ratings = []
dates = []

# Iterate over each review element
for review in reviews:
    # Extract the review text
    review_text = review.find("p", class_="comment").get_text()
    review_texts.append(review_text.strip())

    # Extract the rating
    rating = review.find("div", class_="rating").get("aria-label")
    ratings.append(rating)

    # Extract the date
    date = review.find("span", class_="rating-qualifier").get_text()
    dates.append(date.strip())

# Create a DataFrame from the extracted data
data = {
    "Review Text": review_texts,
    "Rating": ratings,
    "Date": dates
}
df = pd.DataFrame(data)

# Print the DataFrame
print(df)

# Get the current working directory
path = os.getcwd()

# Save the DataFrame as a CSV file
csv_path = os.path.join(path, "yelp_reviews.csv")
df.to_csv(csv_path, index=False)

# Close the ChromeDriver instance
driver.quit()

That's what I have so far but I keep getting this error message

WebDriverException                        Traceback (most recent call last)
<ipython-input-11-6c92e956c704> in <cell line: 27>()
     25 
     26 # Create the ChromeDriver instance
---> 27 driver = webdriver.Chrome(service=service, options=chrome_options)
     28 
     29 # Load the Yelp page

5 frames
/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    243                 alert_text = value["alert"].get("text")
    244             raise exception_class(message, screen, stacktrace, alert_text)  # type: ignore[call-arg]  # mypy is not smart enough here
--> 245         raise exception_class(message, screen, stacktrace)

WebDriverException: Message: unknown error: no chrome binary at C:\Program Files\Google\Chrome\Application\chrome.exe
Stacktrace:
#0 0x55f8912b24e3 <unknown>
#1 0x55f890fe1c76 <unknown>
#2 0x55f8910085e0 <unknown>
#3 0x55f891007029 <unknown>
#4 0x55f891045ccc <unknown>
#5 0x55f89104547f <unknown>
#6 0x55f89103cde3 <unknown>
#7 0x55f8910122dd <unknown>
#8 0x55f89101334e <unknown>
#9 0x55f8912723e4 <unknown>
#10 0x55f8912763d7 <unknown>
#11 0x55f891280b20 <unknown>
#12 0x55f891277023 <unknown>
#13 0x55f8912451aa <unknown>
#14 0x55f89129b6b8 <unknown>
#15 0x55f89129b847 <unknown>
#16 0x55f8912ab243 <unknown>
#17 0x7ff7aa929609 start_thread```

Solution

  • Check the following things:

    1. Check if the specified file exists. Python:
    import os
    print(os.path.exists(chrome_binary_path)) # checks if the path exists.
    # prints 'True' if the file exists, else it prints 'False'
    

    or one-line:

    print(__import__("os").path.exists(chrome_binary_path))
    
    1. Check if you have permission to open that file. Press [Win] + [R] and paste the file path there. A message box with an error like "Permission denied" should appear if you have no permission to read the file.

    2. Check if the hard drive is corrupted. https://www.avast.com/c-chkdsk-windows