I am now using Python + selenium + Chromedriver and I am trying to log in the my account automatically.
To make my program faster, here is my strategy:
Firstly, disable the picture-loading setting when initialling the driver.(see the code blow)
Since my log in process needs validation code, after fetching the page, I want to enable the picture-loading setting so that I can click the "refresh" button to gain validation code picture.
Here is my code for step 1:
ChromeOption = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images":2}
ChromeOption.add_experimental_option("prefs", prefs)
Driver = webdriver.Chrome(executable_path = './chromedriver', chrome_options = ChromeOption)
However, I don't know how to modify the driver settings. I search the document all day and found nothing.
Thank you all!
You cannot alter the chromedriver settings after it has been spun off because :
It would mean that the existing chromedriver process needs to be killed and re-invoked with the new arguments, which would now mean that your test would need to be started off again.This would essentially mean that your test has to start off again from the first.
Related
Following info on this answer (How to load default profile in Chrome using Python Selenium Webdriver?) I have this piece of code which works fine provided Chrome is not open, otherwise an error is given.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chromeDriverPath = 'C:\\Users\\xxxx\\AppData\\Local\\SeleniumBasic\\chromedriver.exe'
userdatadir = 'C:\\Users\\xxxx\\AppData\\Local\\Google\\Chrome\\User Data'
chromeOptions = webdriver.ChromeOptions()
tmp = f"--user-data-dir={userdatadir}"
chromeOptions.add_argument(tmp) #Path to your chrome profile
driver = webdriver.Chrome(chromeDriverPath, options=chromeOptions)
print(driver.current_url)
If Chrome is already open this is the error: 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
A possible workaround is to (programmatically) copy the Chrome default directory into a new one. This way all settings and cookies are copied as well.
Problem, in my case, is that this directory is >2GB; I can't every time copy so much data... It has no sense.
So is it impossible or can anyone help in opening Chrome with default profile even if it is open?
The answer is you cannot open two session with 1 user-directory. Web-driver won't let you do this, It is like to rename a file in Windows when the file is open.
If you want to open chrome without user-dir, you can simply open with --incognito it will not store anything or use user-dir.
Otherwise you have to close the session with driver.quit() at the end of the test (teardown). So you can run the next test without problem.
Or you can use another user direcotry path, so the sessions will not interfers each other
Updated(21Apr2021)
I found the problem. Thank to #simpleApp to point out the problem. I didn't notice the space character could make a problem.Now it's working well
option.add_argument("--user-data-dir=C:\\Users\\yen.ld\\AppData\\Local\\Google\\Chrome\\User Data\\")
option.add_argument("--profile-directory=Profile 1") #no space
Hello i'm using the newest selenium and python and chrome driver . And i'm facing an issue that. Even though i input correct directory but after run the code then it make other profile name under the same directory. Basically these 2 profiles will co-exist
This is my code and i tried some option but no solution till now..Is there any solution about it? Thank you guys so much
option = webdriver.ChromeOptions()
option.add_argument("--user-data-dir=C:\\Users\\NEO\\AppData\\Local\\Google\\Chrome\\User Data\\")
option.add_argument("--profile-directory= Profile 6")
driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe', options=option)
I would like seleniumbase to use my existing session when it launches vs starting a clean one.
How do I start selenium using an existing session?
webdriver:
driver = webdriver.Chrome('/snap/bin/chromium.chromedriver', options=option)
UPDATE:
This is marked as a duplicate which it is not.
Dear #TodorMinakov this code here didn't meet my expectations. I wanted whenever I run the python program to select the unique opened window and use selenium over that window. I will appreciate your help. – Youssof H
Same issue here. The referred URL does not grab an existing session.
You can use default chrome profile.
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=PATH") #)PATH is path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)
To find path to your chrome profile data you need to type chrome://version/ into address bar.
For ex. mine is displayed as C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default,
If path will not work, try to remove \Default\ so we end up with only C:\Users\pc\AppData\Local\Google\Chrome\User Data.
Better to copy-paste default profile outside, because there no way to use same session (cookies) in two browsers in same time.
This question already has answers here:
Selenium: Point towards default Chrome session
(2 answers)
Closed 3 years ago.
I have Google Chrome already opened where I am doing my own work. I am trying to run a Selenium Chrome driver using the default configuration stored in my C:\\Users\\Himanshu Poddar\\AppData\\Local\\Google\\Chrome\\User Data ddirectory.
But when I am launching Chrome using this default profile, my function never returns which I think is due to my another Chrome window opened on which I am working. Because I can see an alert appearing on my working Chrome window which says "chrome is controlled by test software".
Here is what I have tried
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\Himanshu Poddar\\AppData\\Local\\Google\\Chrome\\User Data")
chrome_path = r"C:\Users\Himanshu Poddar\Desktop\chromedriver.exe"
# This never returns
wd = webdriver.Chrome(chrome_path, chrome_options=options)
The last executing statement webdriver.Chrome never return and prints this to the console
[5972:6048:0614/210846.434:ERROR:cache_util_win.cc(21)] Unable to move the cache: 0
[5972:6048:0614/210846.435:ERROR:cache_util.cc(141)] Unable to move cache folder C:\Users\Himanshu Poddar\AppData\Local\Google\Chrome\User Data\ShaderCache\GPUCache to C:\Users\Himanshu Poddar\AppData\Local\Google\Chrome\User Data\ShaderCache\old_GPUCache_000
[5972:6048:0614/210846.435:ERROR:disk_cache.cc(185)] Unable to create cache
[5972:6048:0614/210846.435:ERROR:shader_disk_cache.cc(623)] Shader Cache Creation failed: -2
Opening in existing browser session.
What was expected:
A different instance of Chrome to be running with the same default profile that is independent of my current chrome window on which I am working.
Note that I tried the suggestions that I was getting for this question and none of them worked for me.
Edit
This issue is somewhat similar to mine but does not contains the solution to this problem.
Thanks to #pcalkins I was able to find an answer for the question. The very first step was to clone your User profile or whatever was there in C:\\Users\\Himanshu Poddar\\AppData\\Local\\Google\\Chrome\\User Data and rename it with some name. In my case I just renamed it with User data - Copy. Next step involves deleting the files that our current user who is using google chrome (in other window) from our cloned copy. Finally run the code as
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
chrome_path = r"C:\Users\Himanshu Poddar\Desktop\chromedriver.exe"
options = Options()
# new clone copy of user data supplied
options.add_argument("user-data-dir=C:\\Users\\Himanshu Poddar\\AppData\\Local\\Google\\Chrome\\User Data - copy")
wd = webdriver.Chrome(chrome_path, chrome_options=options)
Note that the update to my driver wasn't required in this case but its always a good practice to work with latest stable release of the software who knows what dependency problem you may run into.
Though I have cloned the profile and then supplied it to my function. Any answer that does not require copy to be generated and solves it just by including, tweaking, twisting any parameter will be accepted.
I am attempting to scrape websites using selenium-python. I am trying to use firefox driver (since PhantomJS doesn't work on me), but is there way to block the pop-ups when using the firefox driver?
thank you
Even I turn on "disable pop ups" in FireFox manually, it's not working for all website. A work around is switch to pop up window, close it, and switch back to the original window.
Here is the code in python:
# Switch to new window opened
driver.switch_to.window(driver.window_handles[-1])
# Close the new window
driver.close()
# Switch back to original browser (first window)
driver.switch_to.window(driver.window_handles[0])
Sorry for the late answer, but you can use the dom.popup_maximum profile preference, setting it to 0 like this:
from selenium import webdriver
fp = webdriver.FirefoxProfile()
fp.set_preference("dom.popup_maximum", 0)
driver = webdriver.Firefox(firefox_profile=fp)
url = "http://some.url.with.annoying.popups"
driver.get(url)
For a list of other available options, please refer to this answer
Use xvfb in the framebuffer and here is a simple usage.
It provides an X environment for selenium.