How to use undetected chromedriver with Brave - python

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?

Related

Browser Close Immediately | Selenium

I start to learn selenium today. I install chrome driver and firefox driver accoding to version. I try with both chrome and firefox but browsers closes immediately. I also add drivers to PATH. I also try using direct path of driver
Chrome Version: 109.0.5414.120
Selenium Version: 4.8
Python Version: 3.8
Conda Version: 23.1.0
Code:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(r"C:\Users\Sarper\Drivers\chromedriver.exe")
print(selenium.__version__) # Verison 4.8
chrome_browser = webdriver.Chrome(service=service)
chrome_browser.get("https://www.google.com")
Seems like it closes, because your code is done running.
You might try:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(r"C:\Users\Sarper\Drivers\chromedriver.exe")
print(selenium.__version__) # Verison 4.8
chrome_browser = webdriver.Chrome(service=service)
chrome_browser.get("https://www.google.com")
input("Press ENTER to exit\n")
Service need to get the path to the chromedriver.exe as following:
service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=service)
C:\webdrivers\chromedriver.exe here is the actual path to chromedriver.exe on my computer
To keep the browser open set options with detach = true, as wollowing:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("detach", True)
options.add_argument("start-maximized")
service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=service)
from selenium import webdriver
# Selenium Version
print(webdriver.__version__)
# Instantiating the Chrome driver
chrome_browser = webdriver.Chrome()
# Opening the browser to Google website
chrome_browser.get("https://www.google.com")

Selenium with Python not able to fully open the website

I have tried the following code and tried to open the website as mentioned:
driver = webdriver.Chrome(r"..\chromedriver_win32\chromedriver.exe")
driver.get("https://example.com")
The website opens with the Chrome Browser but not with the Selenium using Python.
Please let me know what should I do to open the website completely.
You can run it with chrome options. I am able to launch your application with below code:
from time import sleep
from selenium import webdriver
PATH = "chromedriver path"
option = webdriver.ChromeOptions()
option.add_argument('--disable-blink-features=AutomationControlled')
option.add_argument("start-maximized")
option.add_experimental_option(
"excludeSwitches", ["enable-automation"])
option.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(PATH, options=option)
url = 'https://example.com'
driver.get(url)
driver.maximize_window()
sleep(20)
output:

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

WebDriverException Error when using selenium chrome webdriver with options

I would like to run the chrome webdriver on my MAC with my original chrome profile/options, so that i don't need to write a script to log in to the page in the temporary opened automated driver.
my original code without options was run successfully
DRIVER = 'chromedriver'
driver = webdriver.Chrome(DRIVER)
However, when I try to use options I get the following error, and here is my code
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=/Users/Victor/Library/Application Support/Google/Chrome/Default")
driver = webdriver.Chrome(executable_path = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", chrome_options=options)
selenium.common.exceptions.WebDriverException: Message: Service /Applications/Google Chrome.app/Contents/MacOS/Google Chrome unexpectedly exited. Status code was: 0
So how may I fix this problem please?

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

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

Categories