Selenium chrome use multiple add_experimental_option - python

I want to use 2 experimental options on my selenium chromedriver project, but I don't get enough information about the "add_experimental_option" function, I want to use 2 experimental options "prefs" and "debuggerAddress", and when I try to run it, it gets error, I can only use one of them, how can I use both of them?
opt = Options()
opt.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
opt.add_experimental_option("prefs", { \
"profile.managed_default_content_settings.images": 2
})
error messages
Traceback (most recent call last):
File "exampleselenium.py", line 28, in <module>
driver = webdriver.Chrome(executable_path="chromedriver.exe",options=opt,service_log_path='NUL')
File "C:\Users\Faraaz\anaconda3\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "C:\Users\Faraaz\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\Faraaz\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\Faraaz\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Faraaz\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: cannot parse capability: goog:chromeOptions
from invalid argument: unrecognized chrome option: prefs

ChromeOptions objects
prefs [type: dictionary]: A dictionary with each entry consisting of the name of the preference and its value. These preferences are only applied to the user profile in use.
debuggerAddress [type: string]: An address of a Chrome debugger server to connect to, in the form of <hostname/ip:port>, e.g. '127.0.0.1:38947'
You can add both the experimental_option as follows:
opt = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images" : 2}
opt.add_experimental_option("prefs", prefs)
opt.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', options=opt, service_log_path='NUL')

Related

I can't open a website using selenium with chrome profile

okay so here is my code:
import schedule
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=C:/Users/soham/AppData/Local/Google/Chrome/User Data")
options.add_argument('--profile-directory=Profile 2')
driver = webdriver.Chrome( executable_path="C:\Program Files (x86)\chromedriver.exe", options=options)
driver.get("google.com")
it gives this error:
C:\Users\soham\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/soham/PycharmProjects/pythonProject/main.py
Traceback (most recent call last):
File "C:\Users\soham\PycharmProjects\pythonProject\main.py", line 12, in <module>
driver = webdriver.Chrome( executable_path="C:\Program Files (x86)\chromedriver.exe", options=options)
File "C:\Users\soham\PycharmProjects\pythonProject\venv\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "C:\Users\soham\PycharmProjects\pythonProject\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\soham\PycharmProjects\pythonProject\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\soham\PycharmProjects\pythonProject\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\soham\PycharmProjects\pythonProject\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
okay so sorry if you're reading this cause i'm only writing this part cause it was saying its most code and told me to write something soo yeah....HELP.

selenium.common.exceptions.WebDriverException:unknown error:cannot connect to chrome at 127.0.0.1:9222 from unknownerror:unable to discover open pages

I managed to connect selenium driver to an already opened chrome session with the following method in python
def iniciar_google_previa():
os.system('google-chrome --no-sandbox --remote-debugging-port=9222 --user-data-dir="/home/natanael/.config/google-chrome/Default" &')
time.sleep(2)
options1 = Options()
options1.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver = "/usr/local/bin/chromedriver"
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options1)
return driver
And it worked really good, then I can open any site with driver.get("site_url") to start scraping.
I wanted to try the same with docker containers. Installed all the requierements for the dockerfile, built it and then tried to run it but I get the following error
File "final1.py", line 104, in <module>
iniciar_ciclo()
File "final1.py", line 21, in iniciar_ciclo
driver = iniciar_google_previa()
File "final1.py", line 67, in iniciar_google_previa
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options1)
File "/opt/app-root/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/opt/app-root/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/opt/app-root/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/opt/app-root/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/opt/app-root/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:9222
from unknown error: unable to discover open pages
I thought it was because it was trying to run a GUI so I added
--headless
google-chrome --no-sandbox --headless --remote-debugging-port=9222 --user-data-dir="/home/natanael/.config/google-chrome/Default"
but got the same error.
already tried installing
xvfb
and then
xvfb-run google-chrome --no-sandbox --remote-debugging-port=9222 --user-data-dir="/home/natanael/.config/google-chrome/Default"
which gave the output
DevTools listening on ws://127.0.0.1:9222/devtools/browser/52bbc92b-e096-4897-8661-233ee573edaf
apparentley it executes the chrome session but is still unable to connect to it and I don't know what else to try

Whenever I try to run Chrome using selenium using Python I get this error. has anyone experienced this before?

Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path = r"C:\Users\Dell\Desktop\chromedriver.exe")
driver.get("http:/https://stackoverflow.com/")
Traceback (most recent call last):
File "C:\Users\Dell\Desktop\Intro.py", line 3, in
driver = webdriver.Chrome(executable_path = r"C:\Users\Dell\Desktop\chromedriver.exe")
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in init
RemoteWebDriver.init(
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in init
self.start_session(capabilities, browser_profile)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: failed to write prefs file
This can cause if your C drive has not enough space. So use any tools like Disk Cleaner or CCleaner to free up some space.
Here is my version of the working code.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path="C:\Windows\chromedriver.exe")
driver.get("https://stackoverflow.com/")
Try adding chromedriver to C:\Windows.
Check both chrome and chromedriver having the same version.

Open chrome with specific user profile using selenium on python mac [duplicate]

This question already has answers here:
How to open a Chrome Profile through Python
(2 answers)
How to open a Chrome Profile through --user-data-dir argument of Selenium
(3 answers)
How to use existing login token for telegram web using selenium webdriver
(2 answers)
Closed 2 years ago.
I'm trying use Selenium to open chrome using a specific profile. I located the profile location in chrome://version and used the following code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument(
"user-data-dir=../../Library/Application Support/Google/Chrome/Profile 2/")
driver = webdriver.Chrome(
executable_path='./chromedriver', options=options)
driver.get("https://google.com")
But instead, it throws this error:
Traceback (most recent call last):
File "select_files.py", line 10, in <module>
driver = webdriver.Chrome(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot parse internal JSON template: Line: 1, column: 1, Unexpected token.
I tried using without the /profile 2/ but it still comes up with this error
I am using Python 3, Selenium 1.25.9, Chrome Version 85.0.4183.83, Chrome Driver MacOS Catalina

Chrome Driver Options to Open Default Browser and not a new Window Python

I am Trying to Open the chrome in a configuration where all the extensions as well as in logged-in state.
I came across an answer to remove default from the chrome driver Options Module. However, when I added this and tried to execute the program runs Opens a new window with everything enabled, but it does not proceed to open the URL specified and stays at starting page.
Here is my code:
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
options=Options()
options.add_argument("user-data-dir=C:\\Users\\aman krishna\\AppData\\Local\\Google\\Chrome\\User Data\\")
driver=webdriver.Chrome("C:\\Users\\aman krishna\Desktop\\New folder (3)\chromedriver.exe",chrome_options=options)
driver.get("https://www.google.com")
In this code a new window opens but does not search for www.google.com
After some time an error message is displayed i.e
Traceback (most recent call last):
File "C:/Users/aman krishna/PycharmProjects/untitled9/beautifoulsoup.py", line 5, in <module>
driver=webdriver.Chrome("C:\\Users\\aman krishna\Desktop\\New folder (3)\chromedriver.exe",chrome_options=options)
File "C:\Users\aman krishna\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\chrome\webdriver.py", line 75, in __init__
desired_capabilities=desired_capabilities)
File "C:\Users\aman krishna\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webdriver.py", line 154, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\Users\aman krishna\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\aman krishna\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "C:\Users\aman krishna\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
(Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.3.9600 x86_64)

Categories