Python / Selenium - "chromedriver" executable needs to be in a PATH - python

I was trying to do something with a gecko driver ( Selenium in this case ) and I can't get my chrome driver to work. Knowing that I am on Mac Sierra, and that my script is in the same folder as my chrome driver, here is my code
from selenium import webdriver
import time
url = "https://accounts.google.com/signup/v2/webcreateaccount?flowName=GlifWebSignIn&flowEntry=SignUp"
driver = webdriver.Chrome("chromedriver")
driver.get(url)
Thanks for the help

You can test if it actually is in the PATH, Open your command prompt go to the location of your chromedriver and hit enter. you will get below message:
C:\>
chromedriver.exe
Starting ChromeDriver 76.0.3809.68 (420c9498db8ce8fcd190a954d51297672c1515d5-ref
s/branch-heads/3809#{#864}) on port 9515
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent
access by malicious code.
Then you need to setup PATH like below before you start your test:
driver = webdriver.Chrome('/path/to/chromedriver')

Try this:
from selenium import webdriver
driver= webdriver.Chrome(executable_path="C:/users/usr/Desktop/chromedriver.exe")
url = 'your website'
driver.get(url)
make sure you are using this slash (/) in your file path to the chrome driver (as shown above)

Related

unable to open webpage after user-data-dir argument is added to options

I added a user-data-dir argument to my webdriver options to run selenium through my personal chrome profile. The code opens chrome in the appropriate chrome profile but it is not able to go to "http://www.python.org". The code works perfectly without adding the user-data-dir argument.
from selenium import webdriver
import chromedriver_autoinstaller
chromedriver_autoinstaller.install() #using latest chromedriver
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=C://Users/Name/AppData/Local/Google/Chrome/User Data") #Path to chrome profile
driver = webdriver.Chrome(options=options)
driver.get("http://www.python.org")
You have to mention the profile path like below:
# path of the chrome profile's parent directory
options.add_argument(r"user-data-dir=C:\\Users\\User\\AppData\\Local\\Google\\Chrome\\User Data")
# name of the directory
options.add_argument("--profile-directory=Default") # you can change this directory name, if you want to use some other directory
Before executing the above code, close all of Chrome browser windows and chromedriver.exe.

How can I open websites with selenium/webdriver?

import time
from selenium import webdriver
driver = webdriver.Chrome('C:\Program Files\Google\Chrome\Application\chrome.exe')
driver.get('https://www.facebook.com')
time.sleep(5)
driver.quit()
error code: Executable_path has been deprecated, please pass in a service object.
The code above begins to open a Google Chrome tab but does not pick a user, and it will stop where Google Chrome shows all your users. I've tried using a specific profiles path but I've gotten various errors. If someone is able to resolve this issue I would appreciate it, I would like to open the Chrome tab as a guest.
It looks like your question has two parts. You are trying to figure out the webdriver and the user profile path. Allow me to answer both of these questions for you.
In the latest version of Selenium the executable_path parameter has been deprecated. Service objects containing the executable path are now required. There are two options for this.
Service objects
Option #1: Use your executable path
Append this import to your code:
from selenium.webdriver.chrome.service import Service
Then, include the service object as such:
driver = webdriver.Chrome(service=Service("C:\Program Files\Google\Chrome\Application\chrome.exe"))
Option #2: Let web driver manager handle it
This is great for when the driver becomes outdated. No need to redownload the driver.
First, go to the project directory in your terminal. If you are using PyCharm, there is no need to traverse to the directory, as you are already in the project directory.
Use pip to install web driver manager:
pip install webdriver_manager
Now, there is no need to enter an executable path:
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.get("https://www.facebook.com")
Selecting a user profile
This is fairly simple. First, go to chrome and enter chrome://version/ into the URL address bar. You will see the profile path. It will look like this C:\Users\yourprofile\AppData\Local\Google\Chrome\User Data\Default.
Then, include the following chrome options as such:
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\Users\yourprofile\AppData\Local\Google\Chrome\User Data")
options.add_argument(r"--profile-directory=Default")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
This works for me.
The service is the path to the chrome driver you can download.
The chrome driver can be downloaded here: https://chromedriver.chromium.org/downloads
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
s = Service('/Users/macbook/PycharmProjects/chromedriver')
browser = webdriver.Chrome(service=s)
browser.get('https://www.facebook.com')
time.sleep(5)
browser.quit()
[1]: https://chromedriver.chromium.org/downloads

Selenium Python Windows "cannot connect to the service"

I've trawled the web and SO for a good few hours and have exhausted all solutions I can find. Windows 10 / Chrome 87.0 / Python 3.
from selenium import webdriver
DRIVER_PATH = r'./chromedriver/chromedriver.exe'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
returns:
WebDriverException: Message: Can not connect to the Service ./chromedriver/chromedriver.exe
I have made sure that the chromedriver is added to path
I have added 127.0.0.1 localhost to a clean hosts file in %windir%\System32\drivers\etc
I have tried specifying the driver path explicitly and implicitly
You need to insert the entire path, on windows its like this:
executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe'
This os path...
r'./chromedriver/chromedriver.exe'
...refers to the ChromeDriver executable within the chromedriver sub-directory which is located in the same directory from where your program executes.
By all possible means r'./chromedriver/chromedriver.exe' is not the actual location of the ChromeDriver executable within your system. Hence you see the error.
Solution
Pass the absolute path of the ChromeDriver as follows:
from selenium import webdriver
DRIVER_PATH = r'C:\..\..\chromedriver\chromedriver.exe'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
References
You can find a couple of relevant detailed discussions in:
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver.exe while opening chrome browser
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service error using ChromeDriver Chrome through Selenium Python
Python Selenium “Can not connect to the Service %s” % self.path in linux server

Selenium - Cant interact with web driver

im trying to load a specific chrome profile using Python selenium webdriver, but i cant interact with the driver after assigning the chrome profile. it opens the chrome profile that i wanted, but from there - nothing. i cant do any action. for example - im trying to open Microsoft.com:
This works:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://www.microsoft.com')
But this doesnt work at all:
from selenium import webdriver
import getpass
username = getpass.getuser()
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=C:/Users/'+username+'/AppData/Local/Google/Chrome/User Data/')
driver = webdriver.Chrome(executable_path='C:/Users/'+username+'/Documents/selProject/chromedriver.exe', chrome_options=options)
driver.get('http://www.microsoft.com')
The above code opens chrome, but doesnt go to microsoft.com or any other action.
thanks for reading!
This is likely because you have Chrome open already with that user signed in.
In order to use chrome with that profile, while also running the script you'll need to separate the directories where the profiles are pulled from. That is, move (or copy) the Default profile to another directory that you call to within the user-data-dir argument.
You do following things and check whether it works.
Upgrade python bindings using
pip install -U selenium
for chrome browser download the latest chrome driver "ChromeDriver 2.45"
form http://chromedriver.chromium.org/downloads and write the code
from selenium import webdriver
driver=webdriver.Chrome("Path of the Chromedriver" + "chromedriver.exe" )
driver.get('http://www.microsoft.com')

Selenium server throws error when attempting to run test with chrome or Internet Explorer

I have a python webdriver script which successfully runs the test on the remote server using firefox, however it throws an error when using chrome and internet explorer.
I have added the directory with both drivers to the server's path. I have also tried starting the server using:
java -jar .\selenium-server-standalone-2.45.0.jar -Dwebdriver.ie.driver=.\IEDriverServer.exe -role hub
I consistently get the same error in powershell:
"... - Exception: The path to the driver executable must be set by the webdriver.chrome.driver system property;..."
Working Script:
def setUp(self):
self.wd = webdriver.Remote(
desired_capabilities=DesiredCapabilities.FIREFOX)
Throws Error:
def setUp(self):
self.wd = webdriver.Remote(
desired_capabilities=DesiredCapabilities.CHROME)
What is the culprit of this problem?
You have the Selenium driver for Firefox installed and configured, but not for Chrome. Installing and configuring boils down to this (source):
Setup
ChromeDriver is a separate executable that WebDriver uses to control
Chrome. It is maintained by the Chromium team with help from WebDriver
contributors. If you are unfamiliar with WebDriver, you should check
out their own Getting Started page.
Follow these steps to setup your tests for running with ChromeDriver:
Ensure Chromium/Google Chrome is installed in a recognized location
ChromeDriver expects you to have Chrome installed in the default
location for your platform. You can also force ChromeDriver to use a
custom location by setting a special capability.
Download the ChromeDriver binary for your platform under the downloads section of this site
Help WebDriver find the downloaded ChromeDriver executable
Any of these steps should do the trick:
include the ChromeDriver location in your PATH environment variable
(Java only) specify its location via the webdriver.chrome.driver system property (see sample below)
(Python only) include the path to ChromeDriver when instantiating webdriver.Chrome (see sample below)
So, basically, you need to either set the path to your Chrome drive in the PATH, or instantiate the drive like this:
driver = webdriver.Chrome('/path/to/chromedriver')
Download the Chromedriver
from selenium import webdriver
driver = webdriver.Chrome('C:\\Users\\xxx\\Downloads\\chromedriver_win32\\chromedriver.exe')
driver.get("http://www.seleniumhq.org/")
The code is for JAVA , set the path similarly in python
If the PATH is not set in your environment variable , then set it programmatically as below:
System.setProperty("webdriver.chrome.driver", "Path_to_your_chromedriver.exe");
driver = new ChromeDriver();

Categories