I know this has been asked a lot of times and I have also tried all of the answers but it isn't working for me. I have tried option and PATH method on a simple script to extract title of a webpage and it worked. but with all of this code, it is giving me the said error
import requests
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
HEADERS = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'}
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
PATH = r"C:\Users\hp\Downloads\chromedriver\chromedriver.exe"
driver = webdriver.Chrome(options=options , executable_path=PATH)
def get_current_url(url, jobTitle, location):
driver = webdriver.Chrome()
driver.get(url)
time.sleep(3)
driver.find_element_by_xpath('//*[@Id="text-input-what"]').send_keys(jobTitle)
time.sleep(3)
driver.find_element_by_xpath('//*[@Id="text-input-where"]').send_keys(location)
time.sleep(3)
driver.find_element_by_xpath('/html/body/div').click()
time.sleep(3)
try:
driver.find_element_by_xpath('//*[@id="jobsearch"]/button').click()
except:
driver.find_element_by_xpath('//*[@id="whatWhereFormId"]/div[3]/button').click()
current_url = driver.current_url
return current_url
current_url = get_current_url('https://pk.indeed.com/', 'Python Developer', 'Karachi')
print(current_url)
Traceback (most recent call last):
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 969, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1438, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "e:\de-projects\Indeed-scraper.py", line 31, in <module>
current_url = get_current_url('https://pk.indeed.com/', 'Python Developer', 'Karachi')
File "e:\de-projects\Indeed-scraper.py", line 15, in get_current_url
driver = webdriver.Chrome()
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 89, in __init__
self.service.start()
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home
CodePudding user response:
Instead of
PATH = r"C:\Users\hp\Downloads\chromedriver\chromedriver.exe"
driver = webdriver.Chrome(options=options , executable_path=PATH)
Try
from selenium.webdriver.chrome.service import Service
webdriver_service = Service('C:\Users\hp\Downloads\chromedriver\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service)
CodePudding user response:
You have 2 options.
First, you could add C:\Users\hp\Downloads\chromedriver\chromedriver.exe
to the system path. Follow the guide below.
https://www.architectryan.com/2018/03/17/add-to-the-path-on-windows-10/
Or second, when you use the r string, you need to change the slash like this. It should work if you flip it.
PATH = r"C:/Users/hp/Downloads/chromedriver/chromedriver.exe"
CodePudding user response:
Solution
You can get rid of all driver, versions issues by using ChromeDriverManager
If at all you are using ChromeDriverManager - Webdriver Manager for Python you don't have to explicitly download the
ChromeDriver
as it automatically gets downloaded.If you want to use the downloaded
ChromeDriver
you can avoid usingChromeDriverManager - Webdriver Manager
Using ChromeDriverManager
you can use the following code block:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.maximize_window()
driver.get("https://www.google.com")
Downloading a specific version of ChromeDriver you can use the following code block:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
s = Service('/home/Automation/selenium_driver/chromedriver.exe')
driver = webdriver.Chrome(service=s)
Note: If you are on Linux / MAC O SX
system you need to strip the extension part i.e. .exe
as it is applicable only for windows
platforms.