Home > Net >  Chromdriver UserData configuration with Selenium
Chromdriver UserData configuration with Selenium

Time:04-14

I have this simple function with Selenium

def config():
    path = r'C:\Users\George\Desktop\Bot\User Data'
    options = webdriver.ChromeOptions()
    options.add_argument('user-data-dir='   path)
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    options.add_argument('--disable-blink-features=AutomationControlled')

    driver = webdriver.Chrome(executable_path=r'C:\Users\George\Desktop\Bot\chromedriver.exe', chrome_options=options)
    return driver

Which is working fine and as expected, But am making a simple bot to do tasks over a website that require sessions and cookies and so on.

So what I have to make each now and then, Is to copy the real browser User Data from:

C:\Users\George\AppData\Local\Google\Chrome\User Data

to my random location

'C:\Users\George\Desktop\Bot\User Data'

And it works fine, The reason am doing this is that I want to avoid login, and have the browser that I work with independent with the bot driver browser, And I Usually keep it laid down and never open while its doing my work.

Question

  • Is there a way, That I can automatically get the current session instant of copying Files (like get one open tab with all information attached )?
  • Is there a better way to do this? ( am sure there are plenty of better ways)

Thanks for the help, Any suggestion, Links, Blogs are much appriciated.

CodePudding user response:

You won't be required to copy the real browser User Data from:

C:\Users\George\AppData\Local\Google\Chrome\User Data

to:

C:\Users\George\Desktop\Bot\User Data

each now and then if you point user-data-dir to the real User Data directory as follows:

def config():
    options = webdriver.ChromeOptions()
    # next line you need to replace the random location with actual browser data location
    options.add_argument(r"--user-data-dir=C:\Users\George\AppData\Local\Google\Chrome\User Data") 
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    options.add_argument('--disable-blink-features=AutomationControlled')
    driver = webdriver.Chrome(executable_path=r'C:\Users\George\Desktop\Bot\chromedriver.exe', chrome_options=options)
    return driver

References

You can find a couple of relevant detailed discussion in:

  • Related