我们如何同时使用“无头模式”选项和用户data-data选项?

发布于 2025-02-02 14:51:55 字数 1241 浏览 2 评论 0原文

我想将两个选项中的两个选项用于屏幕快照站点需要登录。

  • 无头模式- 无头
  • 指定用户数据目录- 用户data-data-dir(在Windows上设置FullPath)

,但这似乎无法正常工作,因为无头驱动程序没有保持登录地位。 (在下面的代码中,我使用函数construct_driver和参数headless_flg = true)

我创建了用户数据(和配置文件dir),通过首先从普通驱动程序创建和打开User-data(否,否) 头选项,登录站点,并持续使用。

无 (在以下代码中,我使用函数construct_driver和参数headless_flg = false)

因此,我认为此问题的原因是无头驱动程序无法正确读取用户数据。 是否可以在无头Chrome驱动程序上指定用户数据?

这是我的代码(python):

from selenium import webdriver as wd
from selenium.webdriver.chrome import service as cs

BASE_DIR = "<fullpath of the user data dir to be located>"
DRIVER_VERSION = "101.0.4951.41"
def construct_driver(headless_flg=False, profile_no=1):
    csi = cs.Service(f"{BASE_DIR}/{DRIVER_VERSION}/chromedriver")

    options = wd.ChromeOptions()
    userdata_dir = f"{BASE_DIR}/data/Chrome-{str(profile_no)}"
    if(headless_flg):
        options.add_argument('--headless')
    options.add_argument(f"--user-data-dir={userdata_dir}")
    # profile = "1"
    # options.add_argument(f"--profile-directory=Default")

    driver = wd.Chrome(service=csi, options=options)
    driver.implicitly_wait(10)
    return driver

I want to use both of the two options for screenshot sites need login.

  • headless mode --headless
  • specify user data directory --user-data-dir (set fullpath on Windows)

But this seems not to work correctly because headless driver does not hold login status.
(in the below code, I used function construct_driver and the argument headless_flg=True)

I created the user-data (and profile dir) that is read from the headless driver by creating and opening the user-data firstly from the normal driver (no headless option, login the sites, and persisted.

I confirmed that the normal driver correctly read the same profile and already logined the site from second driver run.
(in the below code, I used function construct_driver and the argument headless_flg=False)

So, I think the cause of this problem is that headless driver do not read the user-data correctly.
Is there the way to specify the user-data on headless chrome driver?

This is my code (python):

from selenium import webdriver as wd
from selenium.webdriver.chrome import service as cs

BASE_DIR = "<fullpath of the user data dir to be located>"
DRIVER_VERSION = "101.0.4951.41"
def construct_driver(headless_flg=False, profile_no=1):
    csi = cs.Service(f"{BASE_DIR}/{DRIVER_VERSION}/chromedriver")

    options = wd.ChromeOptions()
    userdata_dir = f"{BASE_DIR}/data/Chrome-{str(profile_no)}"
    if(headless_flg):
        options.add_argument('--headless')
    options.add_argument(f"--user-data-dir={userdata_dir}")
    # profile = "1"
    # options.add_argument(f"--profile-directory=Default")

    driver = wd.Chrome(service=csi, options=options)
    driver.implicitly_wait(10)
    return driver

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧故 2025-02-09 14:51:55

我不熟悉Phyton,而是在C#中管理您的请求。

有两个完全不同的选项和开始序列。

也许这会帮助您一点或给您一个主意。

// hide driver Console? true/false 
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = false;

// change Standard-Download-Folder
ChromeOptions options = new ChromeOptions();
var downloadDirectory = GlobalVars.RootPath + GlobalVars.strSymbol;
options.AddUserProfilePreference("download.default_directory", downloadDirectory);
options.AddUserProfilePreference("download.prompt_for_download", false);
options.AddUserProfilePreference("disable-popup-blocking", "true");

// start Selenium Driver:
webdriver = new ChromeDriver(service, options);

I am not familiar with phyton, but managed your request in C#.

There are two complete different options and the start sequence.

Maybe it will help you a little bit or give you an idea.

// hide driver Console? true/false 
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = false;

// change Standard-Download-Folder
ChromeOptions options = new ChromeOptions();
var downloadDirectory = GlobalVars.RootPath + GlobalVars.strSymbol;
options.AddUserProfilePreference("download.default_directory", downloadDirectory);
options.AddUserProfilePreference("download.prompt_for_download", false);
options.AddUserProfilePreference("disable-popup-blocking", "true");

// start Selenium Driver:
webdriver = new ChromeDriver(service, options);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文