V
V
Vitaly2020-06-08 12:50:57
PHP
Vitaly, 2020-06-08 12:50:57

Why cookies are not saved when setting user-data-dir in php-webdriver?

Good afternoon!
I need to authorize on the resource once through selenium and then constantly work with an already authorized browser. I found a very convenient solution - specify the directory before the profile via user-data-dir, so that the browser is authorized every time it starts. But for some reason it doesn't work.

about addCookie in the course, but in this task it is not very suitable.

OS - Ubuntu 18.04
ChromeDriver 83.0.4103.39
selenium-server-standalone-4.0.0-alpha-1.jar I

start everything with the command:

java -Dwebdriver.chrome.driver="/opt/chromium-browser/chromedriver" -Dwebdriver.chrome.whitelistedIps="127.0.0.1" -jar selenium-server-standalone-4.0.0-alpha-1.jar


Here is the code itself:
$options = new ChromeOptions();
$options->addArguments(["--headless", "--no-sandbox", "window-size=1920,1080", "--proxy-server=http://$proxy_ip", "user-agent=$useragent", "--user-data-dir=/home/userdata"]);
$caps = DesiredCapabilities::chrome();
$caps->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $caps, 60000, 60000);

$driver->get("https://domain.ru/cookies.php");

$elem = $driver->findElement(
    WebDriverBy::cssSelector('html')
);

$html = $elem->getAttribute('outerHTML');

var_dump($html);

$driver->quit();


in cookies.php -

if(!isset($_COOKIE['test1'])){
  $values = "DateTime: " . date('m.d.Y H:i:s') . "; Rand: " . rand(111111, 999999);
  setcookie("test1", $values, time()+3600 * 240);
}
var_dump($_COOKIE['test1']);


The profile directory itself in /home/userdata is created with various files after running the script.

And when restarted, it always returns NULL. although if you go through a regular browser, then the cookie is saved without problems.

What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2022-03-25
@tomclancys

--user-data-dir=PATH - Specifies the location of the profiles folder.
--profile-directory=Profile 1 - Specifies the profile folder, default is Default.
--allow-profiles-outside-user-dir - Allows you to specify a custom location for the profiles folder.
--profiling-flush=n - Flush profile data to file after n seconds.
--enable-aggressive-domstorage-flushing - minimizes data loss.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question