#TLDR I want to use brave browser with selenium written in python but can't find any current solutions that work.
This code works
from selenium import webdriver
option = webdriver.ChromeOptions()
option.binary_location = r'C:\Program Files\BraveSoftware\Brave-
Browser\Application\brave.exe'
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe',
options=option)
driver.get("https://www.google.com")
driver.quit()
but executable_path is deprecated:
C:\Users\USER\PycharmProjects\pythonProject\sol2.py:5:
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe', options=option)
Found this on youtube: https://www.youtube.com/watch?v=VMzmVFA-Gps
# import statements
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# Declare variables and setup services
driverService = Service('C:/webdrivers/chromedriver.exe')
# 1. Passes the chromedriver path to the service object
# 2. stores the service object in the s variable
driver = webdriver.Chrome(service=driverService)
# 1. Passes service object driverSerice into the webdriver.Chrome
# 2. Stores object in driver variable
# Body (actually doing stuff)
driver.maximize_window() # maximizes the browser window
driver.get("https://www.google.com") # navigates to google.com
myPageTitle = driver.title
# gets the title of the web page stores in myPageTitle
print(myPageTitle) # prints myPageTitle to Console
assert "Google" in myPageTitle
# checks myPageTitle to ensure it contains Google
# clean up
driver.quit() # closes the browser
When I run this code I get:
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
This code works as long as you allow Google Chrome onto your PC. I don't want Chrome on my PC.
The problem is that I can't figure out how to get selenium to use brave instead of Chrome.
As of this writing I am using the following:
Windows 11 Home
Selenium v4.0.0
Python v3.10
ChromeDriver 95.0.4638.69
Brave Browser Version 1.31.91 Chromium: 95.0.4638.69 (Official Build) (64-bit)
Can some one please explain how to make this work with the current (read nondeprecated) code on brave browser? Thanks for your time.
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")
Note: The DeprecationWarning: executable_path has been deprecated is a harmless warning message which doesn't affects your test execution and you can still ignore it.
References
You can find a couple of relevant detailed discussions in:
How to use Brave web browser with python, selenium and chromedriver?
How to initiate Brave browser using Selenium and Python on Windows
you have to set your path to brave binary.
options.setBinary("Path to brave.exe");
Go through this website:
https://mundrisoft.com/tech-bytes/how-to-execute-selenium-script-in-brave-browser/
Related
What I need
I need to run Selenium with Chrome Profile
Here is my code:
from selenium import webdriver
import time
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=/home/atom/.config/google-chrome") #Path to your chrome profile
options.add_argument('--profile-directory=Profile 1')
driver = webdriver.Chrome(options=options)
driver.get('https://youtube.com')
time.sleep(5)
Problem
The code opens Google Chrome instead of Chromedriver. Profile is loaded. But driver.get doesn't work
Question
How can I use Selenium with Chrome profile?
What chromedriver does is briefly described here. When you call get on the webdriver, you are asking for the site to be opened on the chrome browser. If you want to use brave browser or another browser implementation using the chromium engine, you may specify the location of the browser's binary using:
ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/other/chrome/binary");
You may load a specific profile by using:
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");
Specifying no profile (not including this option should load the default profile).
A complete list of supported options may be found here.
I am stucking with a script to do the following
I want to open my local chrome in which I have several accounts logged in, so for this I do:
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:/Program Files (x86)/Google/Chrome/Application/chrome.exe")
but when I do that
driver.get(url)
doesn't send me to the URL I want.
On the other hand if I do it with
driver = webdriver.Chrome()
driver.get(URL)
Everything goes smoothly but it opens it with the chromedriver.exe and therefore the accounts are not logged in.
Any idea how to open the local chrome and then be able to browse?
Any solutions for the problem
You need to load your user profile while initializing selenium webdriver.
You can do it using
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Path to local user profile") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Path to your local chrome.exe installation", chrome_options=options)
Typing chrome://version/ in chrome address bar will show you the path to user profile.
In automation, when we use Selenium to interact with a web browser, we need a browser driver to communicate with the specific browser. ChromeDriver is the specific driver for Google Chrome. When you use webdriver.Chrome(), it launches the Chrome browser using the ChromeDriver executable. Chromedriver is a bridge between your Selenium script and the Chrome browser exactly as taxi driver is a bridge between you and taxi car.
Chrome uses profiles. By default in tests are not used local profiles of users. The new temporary profile is created instead. However, it is possible to point out the profile which should be used.
You need to pass additional argument user-data-dir to the Chrome options when initialising the Chrome driver, in order to open the local default Chrome profile. If you need to point out specific profile then you should use profile-directory.
Example:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument(r"--user-data-dir=C:/Users/[username]/AppData/Local/Google/Chrome/User Data")
options.add_argument(r'--profile-directory=YourProfileDir')
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get(URL)
To find the profile folder on Windows right-click the desktop shortcut of the Chrome profile you want to use and go to properties -> shortcut and you will find it in the "target" text box.
See also the following answers:
https://stackoverflow.com/a/66179265/21085370
https://stackoverflow.com/a/69251146/21085370
https://stackoverflow.com/a/67389309/21085370
The official documentation is here
I am a complete beginner to selenium and I wrote my first program just to connect to Google.
from selenium import webdriver
path = "C:\\Users\\Home\\Documents\\Python37-32\\Scripts\\Code\\msedgedriver.exe"
driver = webdriver.Edge(path)
driver.get("https://google.com")
print(driver.title)"
My web driver version is 88.0.705.50 (Official build) (64-bit)
I use selenium 3 and I am getting this error while running the code. Also it is opening "data:," for a few seconds then opening Google. Lastly the browser doesn't stay open.
Declare path on a separate line from the import statement
Use raw string in path or double escapes
Code:
from selenium import webdriver
path = r"C:\Users\Home\Documents\Python37-32\Scripts\Code\msedgedriver.exe"
driver = webdriver.Edge(path)
driver.get("https://google.com")
print(driver.title)
What error do you get? It's the default behavior that the browser opens with data:,, then it will direct to the website you want. The browser doesn't stay opened may because the error breaks it.
You can refer to the following steps to automate Edge in python selenium:
Make sure the WebDriver version is the same as the Edge version.
Install the MS Edge Selenium tools using command below:
pip install msedge-selenium-tools selenium==3.141
Sample code:
from msedge.selenium_tools import Edge, EdgeOptions
options = EdgeOptions()
options.use_chromium = True
driver = Edge(executable_path = r"C:\Users\Home\Documents\Python37-32\Scripts\Code\msedgedriver.exe", options = options)
driver.get("https://google.com")
print(driver.title)
def __init__(self):
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=bot_data")
options.add_argument("--headless") # Runs Chrome in headless mode.
options.add_argument('--no-sandbox') # Bypass OS security model
options.add_argument('--disable-gpu') # applicable to windows os only
options.add_argument('start-maximized') #
options.add_argument('disable-infobars')
options.add_argument("--disable-extensions")
# self.driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
self.driver = webdriver.Chrome('chromedriver.exe',
options=options)
self.driver.get('https://www.google.com')
self.wait = WebDriverWait(self.driver, 10)
There is have my codes. I want to change it to headless browser. But i am getting an error.
I added screenshot to show error.
This error message...
ERROR:devtools_http_handler.cc(288)] Error writing DevTools active port to file
...implies that there was an error while writing DevTools active port to the required file.
As per the discussion in How to open a Chrome Profile through Python instead of specifying only the directory name through user-data-dir, you need to pass the absolute path of the user-data-dir.
Solution
So you need to replace the line of code:
options.add_argument("user-data-dir=bot_data")
With:
options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\bot_data")
Reference
You can find a couple of relevant discussions in:
How to use Chrome Profile in Selenium Webdriver Python 3
Selenium: Point towards default Chrome session
Outro
A couple of relevant documentations:
Session isolation in Headless Chrome
headless: Introduce a browser context
Save and restore browser sessions
Headless maintains a different profile folder structure to headful
The Selenium Chrome Webdriver does not load my default Chrome profile.
I already tried many other Stack Overflow solutions including changing the path (and using the local Chrome App, which gives a 'permission denied' Error).
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=~/Library/Application Support/Google/Chrome/Default")
options.add_argument("--profile-directory=Default")
driver = webdriver.Chrome()
driver.get("https://tagmanager.google.com/#/home")
assert "Google Analytics" in driver.title
account_element = driver.find_element_by_css_selector("div.account.card div#149385038 table")
accountPublicId = driver.find_element_by_css_selector("div#149385038 table td.account__public-id")
The result is still the same; it loads only a 'naked' Chrome Webdriver instead of loading the local default profile (I use everyday for work).
Update:
I don't know how or why but now, when I quit Chrome and start Chrome through the Python Script, Google Chrome starts with my profile but does not use the cookies I have in that profile.
I will see if I can add the cookies "manually" with an options.add_arguments.
Try this:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\ProfilePath") ##profile path
w = webdriver.Chrome(executable_path="C:\\chromedriver.exe", chrome_options=options)
you may try this
options.add_argument("user-data-dir=C:\\Test")
did not add anything to the
options arguments (looking via debugger it was an empty list)
options.arguments.append("user-data-dir=C:/test/") managed to update the list of arguments
and it worked.