Thranor
Thranor

Reputation: 453

How to open a Chrome Profile through --user-data-dir argument of Selenium

I am attempting to load a chrome browser with selenium using my existing account and settings from my profile.

I can get this working using ChromeOptions to set the userdatadir and profile directory. This loads the browser with my profile like i want, but the browser then hangs for 60 seconds and times out without advancing through any more of the automation.

If I don't use the user data dir and profile settings, it works fine but doesn't use my profile.

The reading I've done points to not being able to have more than one browser open at a time with the same profile so I made sure nothing was open while I ran the program. It still hangs for 60 seconds even without another browser open.

m_Options = new ChromeOptions();
m_Options.AddArgument("--user-data-dir=C:/Users/Me/AppData/Local/Google/Chrome/User Data");
m_Options.AddArgument("--profile-directory=Default");
m_Options.AddArgument("--disable-extensions");
m_Driver = new ChromeDriver(@"pathtoexe", m_Options);
m_Driver.Navigate().GoToUrl("somesite");

It always hangs on the GoToUrl. I'm not sure what else to try.

Upvotes: 32

Views: 114505

Answers (5)

xnt4ever
xnt4ever

Reputation: 1

For who want to using "--user-data-dir=" and "--profile-directory=".

  1. "--user-data-dir=<Path_to_directory_on_your device>": this path must be exist, if it not exist, create it.

  2. "--profile-directory=<Name_Of_Folder>": Any name you want to name: Defaul/Profile1/Profile3,..., this folder will be created automatic when chrome instance is created.

Here is my example code using Java. It's similar for Python.

    ChromeOptions options = new ChromeOptions();
    userDataDir = "/home/myuser/userDataDir";
    try {
        File file = new File(userDataDir);
        if (!file.exists()) {
            Files.createDirectory(
                    Paths.get(userDataDir));
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    createdUserDataDir = "--user-data-dir=" + userDataDir;
    createdProfileDir = "--profile-directory=" + "profileName";
    chromeOptions.addArguments(createdUserDataDir);
    chromeOptions.addArguments(createdProfileDir);

    WebDriver driver = new ChromeDriver(chromeOptions);
    driver.get("https:www.facebook.com")

(Anyway, I customized "--user-data-dir=" and "--profile-directory=" to use different profiles on each instance of chrome, but this not solved the issue: maximum of attempts, try again later).

Upvotes: 0

Artur INTECH
Artur INTECH

Reputation: 7266

A new data dir or profile will be created automatically if it doesn't exist, so all the manual steps @Thranor suggested are unnecessary. The following should work. If it doesn't, try closing all existing Chrome processes or recreating the data dir.

Using a separate profile for Selenium might indeed be a good idea.

m_Options.AddArgument("--user-data-dir=C:/Users/Me/AppData/Local/Google/Chrome/User Data");
m_Options.AddArgument("--profile-directory=selenium");

Also you may find the following arguments useful:

options.add_argument('--window-size=1920,1080')
options.add_argument('--no-sandbox')
options.add_argument('--disable-extensions')
options.add_argument('--disable-notifications')
options.add_argument('--disable-popup-blocking')
options.add_argument('--disable-web-security')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-site-isolation-trials')
options.add_argument('--disable-features=Translate')
options.add_argument('--restore-last-session') # If you want open tabs to be saved between sessions

Upvotes: 1

Paulo Costa
Paulo Costa

Reputation: 337

If you want to run Chrome using your default profile (cause you need a extension), you need to run your script using another browser, like Microsoft Edge or Microsoft IE and your code will launch a Chrome instance.

My Code in PHP:

namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Chrome\ChromeOptions;

require_once('vendor/autoload.php');

$host = 'http://localhost:4444/';

$options = new ChromeOptions();
$options->addArguments(array(
    '--user-data-dir=C:\Users\paulo\AppData\Local\Google\Chrome\User Data',
    '--profile-directory=Default',
    '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
));
   
$caps = DesiredCapabilities::chrome();
$caps->setCapability(ChromeOptions::CAPABILITY, $options);
$caps->setPlatform("Windows");

$driver = RemoteWebDriver::create($host, $caps);

$driver ->manage()->window()->maximize();

$driver->get('https://www.google.com/');

// your code goes here.

$driver->quit();

Upvotes: 2

gaspa85
gaspa85

Reputation: 1

i guys, in my enviroment with chrome 63 and selenum for control, i have find same problem (60 second on wait for open webpage).

To fix i have find a way by setting a default webpage in chrome ./[user-data-dir]/[Profile]/Preferences file, this is a json data need to insert in "Preferences" file for obtain result

...
"session":{
  "restore_on_startup":4,
  "startup_urls":[
     "http://localhost/test1"
  ]
}
...

For set "Preferences" from selenium i have use this sample code

ChromeOptions chromeOptions = new ChromeOptions();

//set my user data dir
chromeOptions.addArguments("--user-data-dir=/usr/chromeDataDir/");  

//start create data structure to for insert json in "Preferences" file
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("session.restore_on_startup", 4);
List<String> urlList = new ArrayList<String>();
urlList.add("http://localhost/test1");
prefs.put("session.startup_urls", urlList);

//set in chromeOptions data structure 
chromeOptions.setExperimentalOption("prefs", prefs);

//start chrome
ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
//this get command for open web page, response instant
chromeDriver.get("http://localhost/test2")

i have find information here https://chromedriver.chromium.org/capabilities

Upvotes: -1

undetected Selenium
undetected Selenium

Reputation: 193068

As per your code trials you were trying to load the Default Chrome Profile which will be against all the best practices as the Default Chrome Profile may contain either of the following:

  • Extensions
  • Bookmarks
  • Browsing History
  • etc

So the Default Chrome Profile may not be in compliance with you Test Specification and may raise exception while loading. Hence you should always use a customized Chrome Profile as below.

To create and open a new Chrome Profile you need to follow the following steps :

  • Open Chrome browser, click on the Side Menu and click on Settings on which the url chrome://settings/ opens up.
  • In People section, click on Manage other people on which a popup comes up.
  • Click on ADD PERSON, provide the person name, select an icon, keep the item Create a desktop shortcut for this user checked and click on ADD button.
  • Your new profile gets created.
  • Snapshot of a new profile SeLeNiUm

SeLeNiUm

  • Now a desktop icon will be created as SeLeNiUm - Chrome
  • From the properties of the desktop icon SeLeNiUm - Chrome get the name of the profile directory. e.g. --profile-directory="Profile 2"

profile-directory

  • Get the absolute path of the profile-directory in your system as follows :

    C:\\Users\\Thranor\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2
    
  • Now pass the value of profile-directory through an instance of ChromeOptions with AddArgument method along with key user-data-dir as follows :

    m_Options = new ChromeOptions();
    m_Options.AddArgument("--user-data-dir=C:/Users/Me/AppData/Local/Google/Chrome/User Data/Profile 2");
    m_Options.AddArgument("--disable-extensions");
    m_Driver = new ChromeDriver(@"pathtoexe", m_Options);
    m_Driver.Navigate().GoToUrl("somesite");
    
  • Execute your Test

  • Observe Chrome gets initialized with the Chrome Profile as SeLeNiUm

SeLeNiUm

Upvotes: 20

Related Questions