Can I still specify a path to chromedriver using ChromeOptions in Python? - python

I received this error: "WebDriverException: Message: 'chromedriver' executable needs to be in PATH."
The only way I was able to fix it was to manually add one of the locations of chromedriver like this:
driver = webdriver.Chrome("/Users/anncolvin/.rvm/bin/chromedriver")
After Chrome launched, I then received this error: "You are using an unsupported command-line flag: --ignore-certifcate-errors. Stability and security will suffer."
I'd like to try using the following code to address this new error but I don't know how/if I can combine it with manually specifying chromedriver's location?
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["ignore-
certificate-errors"])
browser = webdriver.Chrome(chrome_options=options)
browser.get("http://google.com/")

All you have to do is specify the webdriver location as an argument when calling the Chrome webdriver like so:
chrome_path = r"/Users/anncolvin/.rvm/bin/chromedriver"
browser = webdriver.Chrome(chrome_path, chrome_options=options)

Related

add extension with option headless selenium python

Hi i'd like to add the extension i don't care about cookies on selenium but i have this error :
selenium.common.exceptions.WebDriverException: Message: unknown error: failed to wait for extension background page to load: chrome-extension://fihnjjcciajhdojfnbdddfaoknhalnja/_generated_background_page.html
someone can help me?
Thanks
self.service = Service(self.path)
self.chrome_options = webdriver.ChromeOptions()
self.chrome_options.add_extension("tools/I-don-t-care-about-cookies.crx")
self.chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
self.chrome_options.add_argument('--headless')
self.chrome_options.add_argument('--disable-gpu')
self.chrome_options.add_argument('--no-sandbox')
self.driver = webdriver.Chrome(service=self.service,options=self.chrome_options)```
First, install the extension in your Chrome browser, and check if it works. Then chek the correct extension location. Then you can install it when launching the chromedriver like so:
chrome_options = Options()
[...]
chrome_options.add_argument('--load-extension=/home/user/.config/chromium/Default/Extensions/cjpalhdlnbpafiamejdnhcphjbkeiagm/1.43.0_0')
Selenium documentation can be found at https://www.selenium.dev/documentation/

Whenever I try to call driver window with the help of driver.get() function it throws error in selenium python

Whenever I type the following code, it throws an error and I am not able to connect to my driver window:
driver = webdriver.Chrome("chromedriver.exe")
Error message: InvalidArgumentException
Download chromedriver.exe
Set executable_path to the location where your chromedriver is located.
replace driver = webdriver.Chrome("chromedriver.exe") to
driver = webdriver.Chrome()
Chekc It will work or not.
for more info check below link
https://stackoverflow.com/a/42478941/6559285

How to use Chrome Profiles through Headless Chrome using Selenium and Python

def __init__(self):
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=bot_data")
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")
# self.driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
self.driver = webdriver.Chrome('chromedriver.exe',
options=options)
self.driver.get('https://www.google.com')
self.wait = WebDriverWait(self.driver, 10)
There is have my codes. I want to change it to headless browser. But i am getting an error.
I added screenshot to show error.
This error message...
ERROR:devtools_http_handler.cc(288)] Error writing DevTools active port to file
...implies that there was an error while writing DevTools active port to the required file.
As per the discussion in How to open a Chrome Profile through Python instead of specifying only the directory name through user-data-dir, you need to pass the absolute path of the user-data-dir.
Solution
So you need to replace the line of code:
options.add_argument("user-data-dir=bot_data")
With:
options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\bot_data")
Reference
You can find a couple of relevant discussions in:
How to use Chrome Profile in Selenium Webdriver Python 3
Selenium: Point towards default Chrome session
Outro
A couple of relevant documentations:
Session isolation in Headless Chrome
headless: Introduce a browser context
Save and restore browser sessions
Headless maintains a different profile folder structure to headful

Selenium with Firefox or Chrome profile

I am trying to load a profile to selenium so that I don't have to keep log in to the website that selenium is about to visit. I am running it with Python on my Mac.
In the Firefox version, I use the below code:
def create_selenium_FF():
profile = webdriver.FirefoxProfile('/Users/Victor/Library/Application Support/Firefox/Profiles/z3ay0enb.default')
driver = webdriver.Firefox(profile)
return driver
It can successfully start Firefox, but it doesn't have the log in info of the website that it visits, however I check in the automated Firefox browser using about:profiles, it does recognise the profile that I feed it.
In the Chrome version, I use the below code, notice I make a local copy of the profile already.
def create_selenium_chrome():
DRIVER = 'chromedriver'
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=/Users/Victor/Library/Application Support/Google/Chrome2")
options.add_argument("--profile-directory=Default")
driver = webdriver.Chrome(DRIVER, options=options)
return driver
It can also start Chrome, and looks like it has my profile, but it raises an error:
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
How can I get it working please?
I just solved the problem !
So here is my code :
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\%username%\\AppData\\Local\\Google\\Chrome\\User Data 2")
driver = webdriver.Chrome(executable_path=path,options=options)
driver.get("https://www.google.com")
The thing is, you'll get this error if there already is chrome opened on your computer !
So, i just copy / paste the folder User Data and rename the pasted one into User Data 2 so my chrome works with user data and selenium with user data 2 I guess.
I know that you've been waiting for a long time and I don't know if u still need this but here you got !!

Getting Chrome to launch via Selenium

I am having issues getting an instance of a Chrome browser from selenium in python. I'm using Windows 8. I have downloaded the chromedriver binary and added it to my path but I get the following error in Python:
selenium.common.exceptions.WebDriverException: Message: 'ChromeDriver executable needs to be available in the path.
This error occurs for the following line:
driver = webdriver.Chrome(executable_path='path\to\chromedriver_win32_2.0')
Two ways to set it, you somehow mixed up.
Put the chromedriver.exe's path into PATH (on Windows), so your PATH setting is correct, but you need to call the default constructor.
driver = webdriver.Chrome()
Specify the path in webdriver.Chrome(executable_path='some path'). Here you need the full path to the executable, not the directory.
webdriver.Chrome(executable_path=r'C:\Users\HaranKumar\Downloads\chromedriver_win32_2.0\chromedriver.exe')
Choose either one you want.
Assuming that your path is correct, make sure that you include the chromedriver itself: chromedriver.exe
I used the following and it worked! Thanks!
driver = webdriver.Chrome(executable_path=r'C:\chromedriver.exe')
#put your own path between the ''
Even if you have chromedriver.exe in the PATH, its necessary to have chromedriver.exe in the folder where your executable scripts are present(atleast so is the case when it comes to python scripts)
for python(selenium) you will need:
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
then put your chromedriver.exe path in. the "r" is just to prevent it from detecting the \ and causing errors in python
PATH = r"C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
options = webdriver.ChromeOptions()
you can now order the driver to get websites
driver.get('http://www.google.com')
Update 2021
For Python
I got Selenium to open my default Chrome browser with my profile using this:
options.add_argument("--user-data-dir=C:\\Users\\Sams\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument('--profile-directory=Default')
Go to: chrome://version/
Look for your "Profile Path"
profile path image
Copy your Profile Path and replace "options.add_argument("--user-data-dir={YOUR PROFILE PATH}")"
The second argument may also look different. Mine so happens to be "Default". For other it may be "Profile 1" or "Profile X" X being an incrementing number.
Close all your Chrome browsers before running. Because the Chrome Driver cannot run an automated browser alongside the rest of your other tabs.
Here is my entire Selenium Config. Hopefully it helps someone.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.remote.webdriver import WebDriver
options = Options()
options.add_argument("--window-size=1920,1080")
options.add_argument("--log-level=3")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
# The 2 arguments below will use your main browser.
options.add_argument("--user-data-dir=C:\\Users\\Sams\\AppData\\Local\\Google\\Chrome\\User Data") # profile path (C)
options.add_argument('--profile-directory=Default')
options.headless = False # To show Chrome or not to show?
PATH = (r"C:\Users\Sams\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\selenium\webdriver\chrome\chromedriver.exe")
CHROMEDRIVER_PATH = PATH
driver = webdriver.Chrome(CHROMEDRIVER_PATH, options=options)
I recently ran into this error and I found three main solutions for this chromedriver not in path error
download a library that does it - webdriver_manager or chromedriver_autoinstaller both work
add it to path in your program
add chromedriver executable to your system PATH
Update 2016
The following solution works for me, with WebDriver 3.0.1, Chrome Driver 2.25.426923, Window 7
System.setProperty("webdriver.chrome.driver","D:\\workspace\\chromedriver.exe");
WebDriver driver;
driver = new ChromeDriver();
*Note:
Chrome Driver
See also: http://www.frontendtest.org/blog/path-executable-chrome/

Categories

Resources