My chromium version is 87.0.4280.88
My brave browser version is 87.0.4280.101
I tried a lot of codes but they didn't work.
Code:
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")
Error traceback:
Traceback (most recent call last):
File "c:\Users\MOHSEN\Desktop\test\Untitled-1.py", line 5, in <module>
driver = webdriver.Chrome(
TypeError: __init__() got an unexpected keyword argument 'options'
To initiate a brave Browsing Session using Selenium driven WebDriver 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")
Browser Snapshot:
Related
I'm using Python with Selenium but I need to use it with extension (and probably with cookies). Extension is uploaded from ZIP file and I need to change something in this extension settings after instalation so it will be hard to reupload extension every start of project. Is there aby option to use it like that? I was trying to use profile from normal chrome but it doesn't work for me.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
service = Service('D:\\chromedriver.exe') # your driver path
chrome_options = Options()
chrome_options.add_argument \
(r"--user-data-dir=C:\\Users\\czarn\\AppData\\Local\\Google\\Chrome\\User Data") # your chrome user data directory
chrome_options.add_argument(r'--profile-directory=Member') # the profile with the extensions loaded
window = webdriver.Chrome(service=service, options=chrome_options)
I have this error:
Traceback (most recent call last):
File "C:\Users\czarn\PycharmProjects\trening\main.py", line 16, in <module>
window = webdriver.Chrome(service=service, options=chrome_options)
TypeError: __init__() got an unexpected keyword argument 'service'
As you mentioned, you can use an already existing Chrome Profile.
Example code where the paths are referring to my machine but it should be easy for you to adapt to your use case.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
service = Service('C:\\Users\\nicoc\\PycharmProjects\\WriteAI\\chromedriver.exe') # your driver path
chrome_options = Options()
chrome_options.add_argument \
(r"--user-data-dir=C:\\Users\\nicoc\\AppData\\Local\\Google\\Chrome\\User Data") # your chrome user data directory
chrome_options.add_argument(r'--profile-directory=Default') # the profile with the extensions loaded
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.get("https://google.com/")
The code works for Selenium v4 (it's still in beta but it works fine)
Here's my code:
from selenium import webdriver
from selenium.webdriver import Chrome, chrome
def startchrome(url):
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches',['enable-logging'])
driver = Chrome(options=options)
driver.get(url)
this is for backend.py
and I run chrome by importing backend to main script and running by:
backend.startchrome(url)
Chrome opens but main crashes (says is not responding)
Thanks
modify startChrome() method a bit :
def startchrome(url):
executable_path = r"C:\\Users\\Inc\\Desktop\\Selenium+Python\\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
options.add_experimental_option("useAutomationExtension", False)
driver = webdriver.Chrome(executable_path, options=options)
driver.get("http://www.google.com")
replace google.com with url string
options = FirefoxOptions()
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=options, executable_path='/Users/toprak/Desktop/geckodriver')
driver.get("https://twitter.com/login?lang=en")
When I try to run my code, I get this error:
Warning (from warnings module):
File "/Users/toprak/Desktop/topla.py", line 19
driver = webdriver.Firefox(firefox_options=options, executable_path='/Users/toprak/Desktop/geckodriver')
DeprecationWarning: use options instead of firefox_options
Traceback (most recent call last):
File "/Users/toprak/Desktop/topla.py", line 19, in <module>
driver = webdriver.Firefox(firefox_options=options, executable_path='/Users/toprak/Desktop/geckodriver')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 137, in __init__
if options.binary is not None:
AttributeError: 'Options' object has no attribute 'binary'
When I delete the lines which are about options and take out "firefox_options=options", the code works fine. What should I do to fix this?
Instead of using firefox_options object you need to use options object. Additionally you need to use the headless attribute. So your effective code block will be:
options = FirefoxOptions()
options.headless = True
driver = webdriver.Firefox(executable_path='/Users/toprak/Desktop/geckodriver', options=options)
driver.get("https://twitter.com/login?lang=en")
References
You can find a couple of relevant detailed discussions in:
How to make Firefox headless programmatically in Selenium with Python?
The --headless argument works fine in Firefox (geckodriver) these days.
If you're getting the error mentioned in the title, then you're probably accidentally creating or passing a Chrome-based Options object rather than a Firefox-based Options object.
To avoid that mistake, it's best to create an import alias for both of them so that they're easier to distinguish.
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
chrome_options = ChromeOptions()
chrome_options.add_argument('--headless')
chrome_driver = webdriver.Chrome(executable_path = r"..\mypath\chromedriver.exe", options=chrome_options)
firefox_options = FirefoxOptions()
firefox_options.add_argument('--headless')
firefox_driver = webdriver.Firefox(executable_path = r"..\mypath\geckodriver.exe", options=firefox_options)
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")
I am getting error issues all of a sudden with selenium and the chromedriver. I haven't changed a single thing yet I am met with these error messages. The script literally worked hours ago and now without any tweaks its not working.
traceback (most recent call last):
File "email.py", line 3, in <module>
from selenium import webdriver
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\__init__.py", line 18, in <module>
from .firefox.webdriver import WebDriver as Firefox # noqa
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 20, in <module>
import http.client as http_client
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 71, in <module>
import email.parser
File "C:\Users\Doe Labs\Desktop\Austin\Scripts\email.py", line 12, in <module>
options = webdriver.ChromeOptions()
Here is my corresponding code:
import pyautogui
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
caps = DesiredCapabilities().CHROME
#caps["pageLoadStrategy"] = "eager"
options = webdriver.ChromeOptions()
options.add_argument(r'load-extension=C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\facebookpixelhelper')
#options.add_argument('start-fullscreen')
options.add_argument('disable-infobars')
driver=webdriver.Chrome(desired_capabilities = caps, executable_path=r'C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\chromedriver', chrome_options=options)
driver.get('http://www.doelabs.com/')
driver.maximize_window()
Even more strange is that when open new terminal, load python, and type from selenium import webdriver, i dont get any errors. But, when I navigate to the folder where the script lives, and load python and type from selenium import webdriver, i get the error message that shows up above. I hope this can give some insight into my current predicament.
A few words about the solution :
email is a reserved word / keyword in Python Language, avoid using the word email within user defined filename/methods/classes.
pageLoadStrategy as eager is yet to be implemented in ChromeDriver, use either none or normal instead as per your requirement.
To maximize the Chrome Browser Window instead of maximize_window() use the argument start-maximized through ChromeOptions()
To load an extension use ChromeOptions as follows :
options.addExtensions(new File("/path/to/extension.crx"));
Here are the four methods to initialize Chrome Browser through ChromeDriver :
Vanila Method :
from selenium import webdriver
driver = webdriver.Firefox(r'C:\path\to\chromedriver.exe')
driver.get('http://www.doelabs.com/')
print("Page Title is : %s" %driver.title)
driver.quit()
Arguments as ChromeOptions :
from selenium import webdriver
options = webdriver.ChromeOptions()
options.addExtensions(new File("C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\facebookpixelhelper.crx"));
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe')
driver.get('http://www.doelabs.com/')
print("Page Title is : %s" %driver.title)
driver.quit()
Capabilities as DesiredCapabilities :
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().CHROME.copy()
caps["pageLoadStrategy"] = "normal"
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', desired_capabilities=caps)
driver.get('http://www.doelabs.com/')
print("Page Title is : %s" %driver.title)
driver.quit()
Arguments as ChromeOptions and Capabilities as DesiredCapabilities :
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().CHROME.copy()
caps["pageLoadStrategy"] = "normal"
options = webdriver.ChromeOptions()
options.addExtensions(new File("C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\facebookpixelhelper.crx"));
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe', desired_capabilities=caps)
driver.get('http://www.doelabs.com/')
print("Page Title is : %s" %driver.title)
driver.quit()
You might want to change
executable_path=r'C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\chromedriver',
to
executable_path=r'C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\chromedriver.exe',
You seem to have missed .exe, the extension of the executable file.