Selenium Python Chrome open with def options
Question:
I want open Google Chrome, like its self, the chromedriver open it without my cookies, my passwords, my history and all that staff.
i tried to play with the option, and search all over the web for solution,
didn’t got one,
plus i tried
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
opt = webdriver.ChromeOptions()
opt.add_arguments("--user-data-dir=C:UsersBarAppDataLocalGoogleChromeUser Data")
driver = webdriver.Chrome(opt)
driver.get("https://www.google.com/")
but it didn’t work it says:
C:UsersBarAppDataLocalProgramsPythonPython35-32python.exe C:/Users/Bar/PycharmProjects/yad2/Webdriver.py
File "C:/Users/Bar/PycharmProjects/yad2/Webdriver.py", line 7
opt.add_arguments("--user-data-dir=C:UsersBarAppDataLocalGoogleChromeUser Data")
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 18-19: truncated UXXXXXXXX escape
Process finished with exit code 1
Answers:
AttributeError: ‘Options’ object has no attribute ‘add_arguments’
It should be add_argument
instead of add_arguments
. You should try as :-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opt = webdriver.ChromeOptions()
opt.add_argument("user-data-dir=C:UsersBarAppDataLocalGoogleChromeUser Data")
AttributeError: ‘Service’ object has no attribute ‘process’
Now you need to set this opt
into chrome_options
and pass it into ChromeDriver
as :-
driver = webdriver.Chrome(chrome_options=opt)
driver.get("https://www.google.com/")
Edited :- You need to download latest chromedriver.exe
executable from here and extract this zip into at any location of your system and provide this path location with executable chromedriver.exe
as executable_path="path/to/chromedriver.exe"
and Initialize ChromeDriver
as :-
driver = webdriver.Chrome(executable_path="path/to/chromedriver.exe", chrome_options=opt)
driver.get("https://www.google.com/")
if you are getting the following error
SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 16-17: truncated UXXXXXXXX escape
change all the backslashes to forward slashes like below
opt.add_argument("user-data-dir=C:UsersBarAppDataLocalGoogleChromeUser Data")
opt.add_argument("user-data-dir=C:/Users/Bar/AppData/Local/Google/Chrome/User Data")
A 2022 update, this worked to me:
from selenium.webdriver.chrome.options import Options
op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)
Hope it will work for you too!
I want open Google Chrome, like its self, the chromedriver open it without my cookies, my passwords, my history and all that staff.
i tried to play with the option, and search all over the web for solution,
didn’t got one,
plus i tried
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
opt = webdriver.ChromeOptions()
opt.add_arguments("--user-data-dir=C:UsersBarAppDataLocalGoogleChromeUser Data")
driver = webdriver.Chrome(opt)
driver.get("https://www.google.com/")
but it didn’t work it says:
C:UsersBarAppDataLocalProgramsPythonPython35-32python.exe C:/Users/Bar/PycharmProjects/yad2/Webdriver.py
File "C:/Users/Bar/PycharmProjects/yad2/Webdriver.py", line 7
opt.add_arguments("--user-data-dir=C:UsersBarAppDataLocalGoogleChromeUser Data")
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 18-19: truncated UXXXXXXXX escape
Process finished with exit code 1
AttributeError: ‘Options’ object has no attribute ‘add_arguments’
It should be add_argument
instead of add_arguments
. You should try as :-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opt = webdriver.ChromeOptions()
opt.add_argument("user-data-dir=C:UsersBarAppDataLocalGoogleChromeUser Data")
AttributeError: ‘Service’ object has no attribute ‘process’
Now you need to set this opt
into chrome_options
and pass it into ChromeDriver
as :-
driver = webdriver.Chrome(chrome_options=opt)
driver.get("https://www.google.com/")
Edited :- You need to download latest chromedriver.exe
executable from here and extract this zip into at any location of your system and provide this path location with executable chromedriver.exe
as executable_path="path/to/chromedriver.exe"
and Initialize ChromeDriver
as :-
driver = webdriver.Chrome(executable_path="path/to/chromedriver.exe", chrome_options=opt)
driver.get("https://www.google.com/")
if you are getting the following error
SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 16-17: truncated UXXXXXXXX escape
change all the backslashes to forward slashes like below
opt.add_argument("user-data-dir=C:UsersBarAppDataLocalGoogleChromeUser Data")
opt.add_argument("user-data-dir=C:/Users/Bar/AppData/Local/Google/Chrome/User Data")
A 2022 update, this worked to me:
from selenium.webdriver.chrome.options import Options
op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)
Hope it will work for you too!