我们如何同时使用“无头模式”选项和用户data-data选项?
我想将两个选项中的两个选项用于屏幕快照站点需要登录。
- 无头模式
- 无头
- 指定用户数据目录
- 用户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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不熟悉Phyton,而是在C#中管理您的请求。
有两个完全不同的选项和开始序列。
也许这会帮助您一点或给您一个主意。
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.