3

I'm trying to do some chrome automation using selenium on python, and I want to have my login info saved for youtube, so that I don't have to automate the login process. I read that the way to have chrome read my saved info is by using the user-data-dir option, but I can't get it to work.

I have looked at some questions here, such as this, and they seem to indicate that the correct usage of the argument user-data-dir in selenium for chrome is the profile path inside: C:\Users\adassa\AppData\Local\Google\Chrome\User Data. Such as Default or Profile 1. I can't seem to get the intended result using this kind of path. The only path that works for me is the User Data folder itself.

So when I do:

chrome_options.add_argument("--user-data-dir=" + r'C:\\Users\\adassa\AppData\\Local\\Google\\Chrome\\User Data')

The chrome instance is opened with the preferences set for the default profile. But that only works correctly when I close all other instances of chrome, otherwise it raises the error: 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

Which I believe is caused by my manual chrome instance blocking access to the info on the folder because I'm using it. I read that I can create a new chrome profile, so that it doesn't conflict with the one I normally use, but if I create a new profile manually on chrome, and use the profile path, as in:

chrome_options.add_argument("--user-data-dir=" + r'C:\\Users\\adassa\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1')

the new instance doesn't have the info I saved to the profile. That happens even if I use as path the User Data\Default folder. I have also tried different combinations using the option profile-dir, with no success.

After reading the docs, I tried using a folder that does not exist. In that case selenium does create the new folder, but when I try to login to youtube trough that selenium created instance, in order to save the info for the browser to use on the next time, I get this message (translation mine):

It was not possible to login. This browser or app may not be safe.

I would love to find a way to have my data saved so that I don't have to login everytime, and also not have to close all other chrome instances when running my script. Any help is appreciated.

1 Answers1

5
chrome_options.add_argument("--user-data-dir=C:\\Users\\adassa\AppData\\Local\\Google\\Chrome\\User Data")
chrome_options.add_argument("--profile-directory=Profile 1")

as the name says usr-dir is for user-data folder and profile-directory is to specify the profile.

if profile folder is default , you don't have to specify that chrome takes it by default else specify it "--profile-directory=Profile 1"

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • For a moment I tought that I had just written `profile-directory` as `profile-dir`, but no, retested with `profile-directory`, it works for getting the Profile's info, but I still get the `user data directory is already in use` error – Thiago Pereira Maia Mar 05 '21 at 01:28
  • 1
    You have to copy the entire used dir folder to a new folder . And use that . – PDHide Mar 05 '21 at 01:54
  • I see. That's unfortunate, the folder ends up being really big (almost 3GB), and even doing that, it doesn't allow me to open multiple instances of selenium using that info. Isn't there a way to open that info in a 'read only' way? – Thiago Pereira Maia Mar 05 '21 at 18:00
  • User data is to store information like cache cookies etc so can't be made read only – PDHide Mar 05 '21 at 21:58