How can I open websites with selenium/webdriver? - python

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

Related

Message: 'geckodriver' executable needs to be in PATH - GoogleCloud

I just created a discord bot and its job is that scraping some data from web and do that help with selenium. On my own computer it works perfectly and i decided to upload it on any cloud server and make it online 7/24. So i found GoogleCloud for free and created an ubuntu VM. I installed required modules and just moved chromedriver to the path that test.py is located. When i type python test.py and execute, it works fine untill any function that has driver.chrome() statement works. Thanks for commentsubuntu terminal
Specify The executable_path Parameter Value When Initialize Firefox like belwo.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://www.google.com")

Selenium with brave a chromium based browser(Version 1.33.106 Chromium: 96.0.4664.110 (Official Build) (64-bit)

I'm a beginner level python programmer,
I am currently working on a browser automater using selenium,
but currently i'am using brave version 96.0.4664.45
and my chrome driver is'nt working properly, whereas geckodriver is working fine with firfox
error here---> Errors with selenium library in python path and all correct with my side
Pls help me as soon as possible
To initiate a brave browsing context you need to:
Use the binary_location attribute to point to the brave binary location.
Use the chromedriver executable to initiate the brave browser.
Code block:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
option = webdriver.ChromeOptions()
option.binary_location = r'C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application\brave.exe'
driverService = Service('C:/Users/.../chromedriver.exe')
driver = webdriver.Chrome(service=driverService, options=option)
driver.get("https://www.google.com")
References
You can find a couple of relevant detailed discussions in:
How to use Brave web browser with python, selenium and chromedriver?
Use Selenium with Brave Browser pass service object written in python
How to initiate Brave browser using Selenium and Python on Windows
I suggest to try webdriver_manager
https://github.com/SergeyPirogov/webdriver_manager.
It helps to setup path to chromedriver.
For Chrome:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
For Chromium:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType
driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())
I suppose some of this will work for Brave.
Also, take a look at this example:
https://gist.github.com/passivebot/91d726bafc1f08eb475dd8384a3f21db

Python Selenium Detach Option Not Working

I want to write a Python script using Selenium and Chrome where Selenium won't close the Chrome browser when the script finishes. From doing a bunch of googling, it looks like the standard solution is to use the detach option. But when I run the following script:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.google.com/")
It opens up Chrome, goes to Google's homepage, and then closes the browser. It's not throwing any errors.
Any idea why it's not working? I'm using the latest version of Google Chrome on Windows 10, and I've got the latest version of the selenium module installed. I couldn't find anything online that said the experimental detach option no longer existed. And I double checked the API, and it looks like it's the right syntax.
I discovered another way to go: start Chrome in remote debugging mode, then connect to it. That way, not only does the browser stay open, but you can also use your existing Chrome profile so you can take advantage of any sites your cookies allow you to access without having to log in every time you run the script.
Here's what you need to do if you're on Windows 10:
Start Google Chrome up remotely, pointed towards your existing user profile and the port you want to use:
cd "C:\Program Files (x86)\Google\Chrome\Application"
chrome.exe -remote-debugging-port=9014 --user-data-dir="%LOCALAPPDATA%\Google\Chrome\User Data"
In your python script, connect to the local port that this version of Chrome is running on:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "localhost:9014")
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://github.com/")
This code worked perfectly for me using : selenium-wire , hope it works for you
from seleniumwire import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(ChromeDriverManager().install(),options=chrome_options)
driver.get("https://www.google.com/")

Deprecation issues when using Selenium with PyCharm CE

I am trying to run Selenium through PyCharm CE on MacOS, and am attempting to run it with Google Chrome.
However, whenever I run the following:
from selenium import webdriver
browser = webdriver.Chrome("/Users/louiscage/PycharmProjects/SeleniumPractice/chromedriver")
browser.get('https://inventwithpython.com')
This is the result:
/Users/louiscage/PycharmProjects/SeleniumPractice/SeleniumPractice.py:3: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
browser = webdriver.Chrome("/Users/louiscage/PycharmProjects/SeleniumPractice/chromedriver")
I had downloaded the Chromedriver and Geckodriver for Mac64, and placed it in the proper directory for my PyCharm project. But I still can't seem to find a solution to this issue. Any help would be greatly appreciated.
As what the warning said: please pass in a Service object, so you should do it like this instead:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service("/Users/louiscage/PycharmProjects/SeleniumPractice/chromedriver")
browser = webdriver.Chrome(service=service)
browser.get('https://inventwithpython.com')
Pass the executable path on the chrome Service class and put it on a variable service. And then pass it to the webdriver chrome.
Link reference here: Selenium Chrome webdriver service
This deprecation issue is appearing on Selenium, Pip and Python updates. For that all you have to do is the following:.
Use the following code & pip install all the python libraries
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
s = Service(ChromeDriverManager().install())
browser = webdriver.Chrome(service=s)
browser.get('https://inventwithpython.com')
The following worked for me in several versions of webdriver.
from selenium.webdriver.chrome.service import Service
CHROME_DRIVER_PATH = "C:\Development\chromedriver.exe"
service = Service(CHROME_DRIVER_PATH)
driver = webdriver.Chrome(service=service)

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')

Categories