How to use Brave web browser with python, selenium and chromedriver? - python

I switched from Google's Chrome to Brave web browser and am having a hard time getting it to work with Brave like it did with Chrome. Brave is based on chromium so I guessed it should not be that hard. I made sure that my Brave and Chromedriver are on the same version like this,
~/some/path $ chromedriver --version
ChromeDriver 76.0.3809.126 (d80a294506b4c9d18015e755cee48f953ddc3f2f-refs/branch-heads/3809#{#1024})
My chromedriver is also in /user/bin,
~/path $ cd /usr/bin/
/usr/bin $ ls | grep chromedriver
chromedriver
And to check the Brave version, I get: Version 0.68.132 Chromium: 76.0.3809.132 (Official Build) (64-bit)
Then I run this code,
from selenium import webdriver
driver = webdriver.Chrome(executable_path='/usr/bin/brave-browser')
driver.get("http://www.python.org")
driver.close()
This opens a Brave window but then instead of getting the page the driver is pointed to, an exception is thrown,
Traceback (most recent call last):
File "webscrap.py", line 3, in <module>
driver = webdriver.Chrome(executable_path='/usr/bin/brave-browser')
File "/home/username/.local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/home/username/.local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 98, in start
self.assert_process_still_running()
File "/home/username/.local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 111, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service /usr/bin/brave-browser unexpectedly exited. Status code was: -11

I finally managed to make it work:
Try this python script (python3.7)
from selenium import webdriver
driver_path = "C:/Users/username/PycharmProjects/chromedriver.exe"
brave_path = "C:/Program Files (x86)/BraveSoftware/Brave-Browser/Application/brave.exe"
option = webdriver.ChromeOptions()
option.binary_location = brave_path
# option.add_argument("--incognito") OPTIONAL
# option.add_argument("--headless") OPTIONAL
# Create new Instance of Chrome
browser = webdriver.Chrome(executable_path=driver_path, chrome_options=option)
browser.get("https://www.google.es")
cheers.

The executable_path key is used to pass the absolute path of the WebDriver binary i.e. the chromedriver executable.
To initiate a Brave browser session additionally you have to pass the absolute location of the brave-browser binary through the binary_location argument of an instance of ChromeOptions.
So the effective code block will be:
from selenium import webdriver
chromedriver_path = '/usr/bin/chromedriver'
brave_path = '/usr/bin/brave-browser'
option = webdriver.ChromeOptions()
option.binary_location = brave_path
browser = webdriver.Chrome(executable_path=driver_path, options=option)
browser.get("https://www.google.es")
References
You can find a couple of relevant detailed discussions in:
DeprecationWarning: use options instead of chrome_options error using ChromeDriver and Chrome through Selenium on Windows 10 system
How to initiate Brave browser using Selenium and Python on Windows
DeprecationWarning: use options instead of chrome_options error using Brave Browser With Python Selenium and Chromedriver on Windows

This also works in windows 10 with Brave browser. I downloaded Chromedriver and put it in the folder with Brave.exe.
from selenium import webdriver
driver_path = "C:\\Users\\5150s\\AppData\\Local\\Programs\\Python\\Python38\\chromedriver.exe"
brave_path = "C:\\Program Files (x86)\\BraveSoftware\\Brave-Browser\\Application\\brave.exe"
option = webdriver.ChromeOptions()
option.binary_location = brave_path
browser = webdriver.Chrome(executable_path=driver_path, options=option)
browser.get("https://www.google.es")

The solutions above gave me some errors. This code removes the executable path and options errors.
Chromedriver is in the pycharm folder.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
driver_path = "C:/Users/johnm/PycharmProjects/chromedriver.exe"
brave_path = "C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe"
s=Service(driver_path)
option = webdriver.ChromeOptions()
option.binary_location = brave_path
browser = webdriver.Chrome(service=s, options=option)
browser.get("https://www.google.es")

Related

How to use undetected chromedriver with Brave

I want to access a website and use the Brave Brower which I got running. The website opens but Cloudflare hinders me from accessing the site:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
chromedriver = r"C:/SeleniumDrivers/chromedriver.exe"
option = webdriver.ChromeOptions()
option.binary_location = "C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe"
s = Service(chromedriver)
driver = webdriver.Chrome(service=s, options=option)
driver.get(url)
Therefore I want to use undetected_chromedriver but I just can not get it to run. Here is what I have tried until now:
import undetected_chromedriver as uc
chromedriver = r"C:/SeleniumDrivers/chromedriver.exe"
brave = 'C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe'
option = uc.ChromeOptions()
option.binary_location = brave
driver = uc.Chrome(driver_executable_path=chromedriver, options=option)
driver.get(url)
This code returns a PermissionError which I can not explain at all.
PermissionError: [Errno 13] Permission denied: 'C:/SeleniumDrivers/chromedriver.exe'
Any tips or solutions that work? Maybe there is a way that lets me access the website and bypasses Cloudlfare with selenium as that Code is running.
have you tried running the process as an administrator?

Trying to load chrome webdriver with extension installed,

I'm trying to load my chrome webdriver with extension installed(following the steps mentioned in 'How to load extension within chrome driver in selenium with python') but unable to find the extension installed can you please help me on this, the code I'm trying is.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_extension('C:/Users/john/Desktop/john/v1.10.0.0.crx')
driver = webdriver.Chrome()
driver.get('https://www.google.co.in')
It just launches and opens the chrome webdriver with google page, I'm trying to open with extension installed is this possible using selenium, can anyone help
Try calling
driver = webdriver.Chrome(chrome_options=chrome_options)
instead of just
driver = webdriver.Chrome()
I can provide you the code written with java, which is working fine
File file = new File("path_to_your_extension");
String path = file.getAbsolutePath();
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(path))
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.co.in")

Can I use Selenium and Brave together?

Rencently I changed my web-browser to Brave, but I was working with chromedriver to automating some tasks
I've read some and I found this post says that the version of ChromeDriver and Brave have to match, but I don't find driver to my current version only for ChromeDriver 80.0.3987.106.
This is my Brave info: VersiĆ³n 1.4.96 Chromium: 80.0.3987.132 (Official Build) (64-bits).
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
option = Options()
option.binary_location = '/usr/bin/brave-browser '
driver_path = '/home/usr_1/Downloads/ML/chromedriver'
driver = webdriver.Chrome(options = option, executable_path = driver_path)
driver.get('https://pypi.org/')
To open a brave Browsing Context using Selenium driven ChromeDriver you can use the following solution:
Code Block:
from selenium import webdriver
option = webdriver.ChromeOptions()
option.binary_location = r'C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application\brave.exe'
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe', options=option)
driver.get("https://www.google.com")

--headless flag no longer works after upgrade to chrome 76/chromedriver 76

I am running python selenium tests using headless chrome. After updating to chrome Version 76.0.3809.87 and chromedriver version ChromeDriver 76.0.3809.68, the chromeOptions that I use (see code sample below)... no longer work. i.e. the browser is launched (non headless) and the resolution settings are also not working
Anyone else seeing this after upgrading to chromedriver 75/76?
chrome_options = {'args': ['headless', '--window-size=1920,1080', 'no-sandbox', '--dns-prefetch-disable','--disable-dev-shm-usage']}
capabilities = {'browserName': 'chrome', 'chromeOptions':chrome_options}
cls.driver = webdriver.Chrome(desired_capabilities=capabilities)
You don't need to use desiredCapabilities to pass these arguments you can use Options instead. I have tested and it works.
Google Chrome - 76.0.3809.87
ChromeDriver - 76.0.3809.68
Selenium - 3.141.0
Python - 3.7.2
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)
driver.get('https://www.google.com')
driver.save_screenshot("screenshot.png")
driver.quit()
Check the screenshot obtained from the headless browser below.

How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?

I'm working on a python script to web-scrape and have gone down the path of using Chromedriver as one of the packages. I would like this to operate in the background without any pop-up windows. I'm using the option 'headless' on chromedriver and it seems to do the job in terms of not showing the browser window, however, I still see the .exe file running. See the screenshot of what I'm talking about. Screenshot
This is the code I am using to initiate ChromeDriver:
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
options.add_argument('headless')
options.add_argument('window-size=0x0')
chrome_driver_path = "C:\Python27\Scripts\chromedriver.exe"
Things I've tried to do is alter the window size in the options to 0x0 but I'm not sure that did anything as the .exe file still popped up.
Any ideas of how I can do this?
I am using Python 2.7 FYI
It should look like this:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu') # Last I checked this was necessary.
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)
This works for me using Python 3.6, I'm sure it'll work for 2.7 too.
Update 2018-10-26: These days you can just do this:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(CHROMEDRIVER_PATH, options=options)
Answer update of 13-October-2018
To initiate a google-chrome-headless browsing context using Selenium driven ChromeDriver now you can just set the --headless property to true through an instance of Options() class as follows:
Effective code block:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe')
driver.get("http://google.com/")
print ("Headless Chrome Initialized")
driver.quit()
Answer update of 23-April-2018
Invoking google-chrome in headless mode programmatically have become much easier with the availability of the method set_headless(headless=True) as follows :
Documentation :
set_headless(headless=True)
Sets the headless argument
Args:
headless: boolean value indicating to set the headless option
Sample Code :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.set_headless(headless=True)
driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe')
driver.get("http://google.com/")
print ("Headless Chrome Initialized")
driver.quit()
Note : --disable-gpu argument is implemented internally.
Original Answer of Mar 30 '2018
While working with Selenium Client 3.11.x, ChromeDriver v2.38 and Google Chrome v65.0.3325.181 in Headless mode you have to consider the following points :
You need to add the argument --headless to invoke Chrome in headless mode.
For Windows OS systems you need to add the argument --disable-gpu
As per Headless: make --disable-gpu flag unnecessary --disable-gpu flag is not required on Linux Systems and MacOS.
As per SwiftShader fails an assert on Windows in headless mode --disable-gpu flag will become unnecessary on Windows Systems too.
Argument start-maximized is required for a maximized Viewport.
Here is the link to details about Viewport.
You may require to add the argument --no-sandbox to bypass the OS security model.
Effective windows code block :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
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")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe')
driver.get("http://google.com/")
print ("Headless Chrome Initialized on Windows OS")
Effective linux code block :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless") # Runs Chrome in headless mode.
options.add_argument('--no-sandbox') # # Bypass OS security model
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path='/path/to/chromedriver')
driver.get("http://google.com/")
print ("Headless Chrome Initialized on Linux OS")
Steps through YouTube Video
How to initialize Chrome Browser in Maximized Mode through Selenium
Outro
How to make firefox headless programmatically in Selenium with python?
tl; dr
Here is the link to the Sandbox story.
Update August 20, 2020 -- Now is simple!
chrome_options = webdriver.ChromeOptions()
chrome_options.headless = True
self.driver = webdriver.Chrome(
executable_path=DRIVER_PATH, chrome_options=chrome_options)
UPDATED
It works fine in my case:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(CHROMEDRIVER_PATH, options=options)
Just changed in 2020. Works fine for me.
So after correcting my code to:
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
options.add_argument('--disable-gpu')
options.add_argument('--headless')
chrome_driver_path = "C:\Python27\Scripts\chromedriver.exe"
The .exe file still came up when running the script. Although this did get rid of some extra output telling me "Failed to launch GPU process".
What ended up working is running my Python script using a .bat file
So basically,
Save python script if a folder
Open text editor, and dump the following code (edit to your script of course)
c:\python27\python.exe c:\SampleFolder\ThisIsMyScript.py %*
Save the .txt file and change the extension to .bat
Double click this to run the file
So this just opened the script in Command Prompt and ChromeDriver seems to be operating within this window without popping out to the front of my screen and thus solving the problem.
The .exe would be running anyway. According to Google - "Run in headless mode, i.e., without a UI or display server dependencies."
Better prepend 2 dashes to command line arguments, i.e. options.add_argument('--headless')
In headless mode, it is also suggested to disable the GPU, i.e. options.add_argument('--disable-gpu')
Try using ChromeDriverManager
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.set_headless()
browser =webdriver.Chrome(ChromeDriverManager().install(),chrome_options=chrome_options)
browser.get('https://google.com')
# capture the screen
browser.get_screenshot_as_file("capture.png")
Solutions above don't work with websites with cloudflare protection, example: https://paxful.com/fr/buy-bitcoin.
Modify agent as follows:
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36")
Fix found here:
What is the difference in accessing Cloudflare website using ChromeDriver/Chrome in normal/headless mode through Selenium Python
from chromedriver_py import binary_path
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--window-size=1280x1696')
chrome_options.add_argument('--user-data-dir=/tmp/user-data')
chrome_options.add_argument('--hide-scrollbars')
chrome_options.add_argument('--enable-logging')
chrome_options.add_argument('--log-level=0')
chrome_options.add_argument('--v=99')
chrome_options.add_argument('--single-process')
chrome_options.add_argument('--data-path=/tmp/data-path')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--homedir=/tmp')
chrome_options.add_argument('--disk-cache-dir=/tmp/cache-dir')
chrome_options.add_argument('user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36')
driver = webdriver.Chrome(executable_path = binary_path,options=chrome_options)
System.setProperty("webdriver.chrome.driver",
"D:\\Lib\\chrome_driver_latest\\chromedriver_win32\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--allow-running-insecure-content");
chromeOptions.addArguments("--window-size=1920x1080");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.setHeadless(true);
ChromeDriver driver = new ChromeDriver(chromeOptions);
chromeoptions=add_argument("--no-sandbox");
add_argument("--ignore-certificate-errors");
add_argument("--disable-dev-shm-usage'")
is not a supported browser
solution:
Open Browser ${event_url} ${BROWSER} options=add_argument("--no-sandbox"); add_argument("--ignore-certificate-errors"); add_argument("--disable-dev-shm-usage'")
don't forget to add spaces between ${BROWSER} options
There is an option to hide the chromeDriver.exe window in alpha and beta versions of Selenium 4.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService # Similar thing for firefox also!
from subprocess import CREATE_NO_WINDOW # This flag will only be available in windows
chrome_service = ChromeService('chromedriver', creationflags=CREATE_NO_WINDOW)
driver = webdriver.Chrome(service=chrome_service) # No longer console window opened, niether will chromedriver output
You can check it out from here. To pip install beta or alpha versions, you can do "pip install selenium==4.0.0.a7" or "pip install selenium==4.0.0.b4" (a7 means alpha-7 and b4 means beta-4 so for other versions you want, you can modify the command.) To import a specific version of a library in python you can look here.
RECENT UPDATE
Recently there is an update performed on headless mode of Chrome. The flag --headless is now modified and can be used as below
For Chrome version 109 and above, --headless=new flag allows us to explore full functionality Chrome browser in headless mode.
For Chrome version 108 and below (till Version 96), --headless=chrome option will provide us the headless chrome browser.
So, let's add
options.add_argument("--headless=new")
for newer version of Chrome in headless mode as mentioned above.
The below works fine for me with Chrome version 110.0.5481.104
chrome_driver_path = r"E:\driver\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_argument('--disable-gpu')
//New Update
options.add_argument("--headless=new")
options.binary_location = r"C:\Chrome\Application\chrome.exe"
browser = webdriver.Chrome(chrome_driver_path, options=options)
browser.get('https://www.google.com')
Update August 2021:
The fastest way to do is probably:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.set_headless = True
driver = webdriver.Chrome(options=options)
options.headless = True is deprecated.

Categories