'Selenium - Java - Chrome doesn't use provided user-data-dir when Headless
I am trying to run some tests with Selenium 4.1.3, Chrome 100 and Java 18. The test requires to use a specific Chrome profile.
My code works perfectly when Chrome is headed but doesn't work (doesn't use the specified Chrome profile) when headless.
My code is below (there's a boolean flag headless, chaning the values makes the program run headed vs. headless - and working vs. not working):
public static void main(String[] args) {
try {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
//System.setProperty("webdriver.chrome.verboseLogging", "false");
ChromeOptions options = new ChromeOptions();
options.addArguments("--window-size=1920,1080");
options.addArguments("--start-maximized");
options.addArguments("start-maximized");
boolean headless = true;
if (headless) {
options.addArguments("--headless");
options.addArguments("--remote-debugging-port=9222");
//options.setHeadless(true);
options.setAcceptInsecureCerts(true);
}
options.addArguments("--log-level=3");
options.addArguments("--silent");
options.addArguments("no-sandbox");
options.addArguments("--no-sandbox");
options.addArguments("enable-automation");
options.addArguments("--disable-infobars");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--disable-gpu");
options.addArguments("--disable-custom-jumplist");
options.addArguments("--allow-no-sandbox-job");
options.addArguments("--lang=it-IT");
String userData = "C:\\Chrome\\fravotto19750619\\";
options.addArguments("--user-data-dir=" + userData);
//String profileDir = "";
//chromiumOptions.addArguments("--profile-directory=" + profileDir);
ChromiumDriver driver = new ChromeDriver(options);
driver.navigate().to("https://www.gmail.com");
String fileName = "test.jpeg";
try {
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File dst = new File(fileName);
try (InputStream in = new FileInputStream(src)) {
try (OutputStream out = new FileOutputStream(dst)) {
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
in.readLine();
driver.quit();
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
I tried to play with options (did many attempts) and in similar thread I did not find any solution to this, I assume that the same code should run similarily when headed or headless but there's a kind of difference I can't find.
Any thought?
Thanks
Solution 1:[1]
One more information: it seems that if I create a profile from headless chromium (I just did some web scraping on the site I want to log on) then it is kept if I run the program again. It seems that the headed and headess profiles do not talk to each other even if seem to work in the same way (or almost in the same way).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Flavio Prova |