Setting user_data_dir

Hi,

I am trying to avoid authenticating to same website everytime I run the robots. Seems like by specifying user_data_dir this can be done. But I haven’t been able to set it into robocorp… is there way to do it or any other way to achieve the same result?

If you are going to run this on CR and containers, it is much more robust to make robot to repeat authentication every time. It is also beneficial for normal cases.

Remember these:

  • from one run to a next, you will get new container, where there is no previous “user directory” available
  • also web applications have sessions (and session timeouts) so if your robot always starts new session, that means that it will behave same way; but otherwise, you have to build logic in robot to handle expired sessions
  • it is more secure to build robot follow pattern “get credentials from safe place” → “login” → “do stuff” → “logout” since then you are not leaving session open and not storing credentials with robot

And to manage your credentials, you should really Use a vault for secrets

And if you really, really, really want to use user_data_dir, then maybe use New Browser with args from RPA.Browser.Playwright library keywords is solution for you (if you are using Playwright).

1 Like

Hi,

Thanks for the insight. My issue is the website I am going to work on will have mfa shortly. I was planning to have multi bots work on the job and having the bots share the authenticated state would help greatly reduce manual intervention

You can extend Selenium with a library like this. Place the file in your robot, then use it with:

*** Settings ***
Library    ExtendedSelenium

*** Tasks ***
Open Website
    Open Site    ${URL}    browser=chrome

and make sure you have in your output/Chrome the user-data content or specify the path explicitly through USER_DATA env var.


To better automate this, you can have one robot that does the login (assisted or not), then the obtained user-data cookies/session can be sent as a new work item in the queues of all the processes you have robots running with this session. Then those robots will pull the work item data and place it in their user-data base for a fresh login.

@cosmin

Thanks for detailed answer. I am stuck on a novice error:
Here is my code:
import os
from pathlib import Path

from RPA.Browser.Selenium import Selenium
from SeleniumLibrary.base import keyword
from selenium import webdriver

class ExtendedSelenium(Selenium):

USER_DATA = Path("output") / "Chrome"
USER_DATA_PATH = os.getenv("USER_DATA", str(USER_DATA)).strip()
                
@keyword
def open_site(self, url, **kwargs):
    print(url)
    options = webdriver.ChromeOptions()
    if self.USER_DATA_PATH:
        options.add_argument(f"user-data-dir={self.USER_DATA_PATH}")
    self.open_browser(
        url=url,
        options=options,
        **kwargs
    )

selobj = ExtendedSelenium()
selobj.open_site(“webiste url”,browser=‘chrome’)

When i run this:
FileNotFoundError Traceback (most recent call last)
~/.robocorp/holotree/2b7547ee0_12a2bf79/lib/python3.7/site-packages/selenium/webdriver/common/service.py in start(self)
75 stderr=self.log_file,
—> 76 stdin=PIPE)
77 except TypeError:

~/.robocorp/holotree/2b7547ee0_12a2bf79/lib/python3.7/subprocess.py in init(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
799 errread, errwrite,
→ 800 restore_signals, start_new_session)
801 except:

~/.robocorp/holotree/2b7547ee0_12a2bf79/lib/python3.7/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
1550 err_msg += ': ’ + repr(err_filename)
→ 1551 raise child_exception_type(errno_num, err_msg, err_filename)
1552 raise child_exception_type(err_msg)

FileNotFoundError: [Errno 2] No such file or directory: ‘chromedriver’: ‘chromedriver’

During handling of the above exception, another exception occurred:

WebDriverException Traceback (most recent call last)
/var/folders/2d/fwpn4y8n5pv1q946ydtzjz100000gn/T/xpython_24808/426359811.py in
1 selobj = ExtendedSelenium()
----> 2 selobj.open_site(“https://www.google.com”,browser=‘chrome’)
3

/var/folders/2d/fwpn4y8n5pv1q946ydtzjz100000gn/T/xpython_24808/1853903439.py in open_site(self, url, **kwargs)
21 url=url,
22 options=options,
—> 23 **kwargs
24 )
25

~/.robocorp/holotree/2b7547ee0_12a2bf79/lib/python3.7/site-packages/SeleniumLibrary/keywords/browsermanagement.py in open_browser(self, url, browser, alias, remote_url, desired_capabilities, ff_profile_dir, options, service_log_path, executable_path)
300 options,
301 service_log_path,
→ 302 executable_path,
303 )
304

~/.robocorp/holotree/2b7547ee0_12a2bf79/lib/python3.7/site-packages/SeleniumLibrary/keywords/browsermanagement.py in _make_new_browser(self, url, browser, alias, remote_url, desired_capabilities, ff_profile_dir, options, service_log_path, executable_path)
329 options,
330 service_log_path,
→ 331 executable_path,
332 )
333 driver = self._wrap_event_firing_webdriver(driver)

~/.robocorp/holotree/2b7547ee0_12a2bf79/lib/python3.7/site-packages/SeleniumLibrary/keywords/browsermanagement.py in _make_driver(self, browser, desired_capabilities, profile_dir, remote, options, service_log_path, executable_path)
721 options=options,
722 service_log_path=service_log_path,
→ 723 executable_path=executable_path,
724 )
725 driver.set_script_timeout(self.ctx.timeout)

~/.robocorp/holotree/2b7547ee0_12a2bf79/lib/python3.7/site-packages/SeleniumLibrary/keywords/webdrivertools/webdrivertools.py in create_driver(self, browser, desired_capabilities, remote_url, profile_dir, options, service_log_path, executable_path)
94 options=options,
95 service_log_path=service_log_path,
—> 96 executable_path=executable_path,
97 )
98

~/.robocorp/holotree/2b7547ee0_12a2bf79/lib/python3.7/site-packages/SeleniumLibrary/keywords/webdrivertools/webdrivertools.py in create_chrome(self, desired_capabilities, remote_url, options, service_log_path, executable_path)
150 service_log_path=service_log_path,
151 executable_path=executable_path,
→ 152 **desired_capabilities,
153 )
154

~/.robocorp/holotree/2b7547ee0_12a2bf79/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py in init(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, keep_alive)
71 service_args=service_args,
72 log_path=service_log_path)
—> 73 self.service.start()
74
75 try:

~/.robocorp/holotree/2b7547ee0_12a2bf79/lib/python3.7/site-packages/selenium/webdriver/common/service.py in start(self)
81 raise WebDriverException(
82 “’%s’ executable needs to be in PATH. %s” % (
—> 83 os.path.basename(self.path), self.start_error_message)
84 )
85 elif err.errno == errno.EACCES:

WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH. Please see ChromeDriver - WebDriver for Chrome

I thought robocorp automatically downloads everything.

Yes, I forgot to add the automatic download part in the customised library, check this updated gist.
I tested it with this robot and is working fine now.

1 Like

Also don’t forget to encrypt cookies/sessions (before sending it as Work Item data) and any other sensitive info outside of the Vault. (you can store the symmetric key in Vault)

1 Like