Home > front end >  How can I open websites with selenium/webdriver?
How can I open websites with selenium/webdriver?

Time:05-31

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.

CodePudding user response:

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)

CodePudding user response:

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
  • Related