Home > Net >  Selenium - Could not start a new session. Response code 500. Message: unknown error: cannot create t
Selenium - Could not start a new session. Response code 500. Message: unknown error: cannot create t

Time:05-11

Problem

I'm attempting to run Selenium from a Java application (more specifically a Talend job). The job runs fine locally, however, when it's deployed to Windows Server 2019 I'm getting the following error:

Could not start a new session. Response code 500. Message: unknown error: cannot create temp dir for unpacking extensions

Here's my Selenium Java code:


Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory",  "C:\\data\\");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);

options.addArguments("--no-sandbox");
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--profile-directory=Default");
options.addArguments("--user-data-dir=C:\\Temp");


WebDriver driver = new ChromeDriver(options);
JavascriptExecutor js = (JavascriptExecutor) driver;
Map<String, Object> vars = new HashMap<String, Object>();
 
   
driver.get("<URI>");
driver.findElement(By.id("<a valid element id for my case>")).click();
Thread.sleep(3000);
driver.close();
driver.quit();

Things I've tried

  • Setting TMP and TEMP environment variables to C:\Temp
  • Setting the chromedriver.exe executable to "Run as Administrator" via the properties dialog of the executable
  • Setting the ---user-data-dir flag to a C:\Temp (as seen above)
  • Deleting the Temp directory (both the default and the current C:\Temp I'm trying)
  • Verified the drive has plenty of free space
  • Restarting the server
  • Downgrading chromedriver.exe to Chrome versions 101, 100, and 99
  • Changing --user-data-dir to C:\Temp\2
  • Setting "Do not delete temp folders upon exit" and "Do not use temporary folders per session" Group Policy Flags to "Enabled"

CodePudding user response:

A 500 error code means that there is a problem with the server. Seeing as you've already tried to restart and check the disk space of the server, the issue might be with the temp directory.

Go to the windows CLI and execute echo %temp%:

Some users are getting C:\TEMP\2 returned. If this (or any other folder name) happens, make the new folder and make sure you are running as admin

CodePudding user response:

Root cause of the issue was actually related to the fact that the Java code was being executed as the SYSTEM user instead of a named account. (Specifically, the Talend Remote Engine was being run as SYSTEM). I switched the code to run as a named account which resolved the error.

  • Related