如何通过 Selenium 的 --user-data-dir 参数打开 Chrome 配置文件 [英] How to open a Chrome Profile through --user-data-dir argument of Selenium

查看:177
本文介绍了如何通过 Selenium 的 --user-data-dir 参数打开 Chrome 配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我现有的帐户和个人资料中的设置加载带有 selenium 的 Chrome 浏览器.

I am attempting to load a chrome browser with selenium using my existing account and settings from my profile.

我可以使用 ChromeOptions 来设置 userdatadir 和配置文件目录.这会像我想要的那样使用我的个人资料加载浏览器,但是浏览器会挂起 60 秒并超时,而没有进一步进行任何自动化.

I can get this working using ChromeOptions to set the userdatadir and profile directory. This loads the browser with my profile like i want, but the browser then hangs for 60 seconds and times out without advancing through any more of the automation.

如果我不使用用户数据目录和配置文件设置,它可以正常工作但不使用我的配置文件.

If I don't use the user data dir and profile settings, it works fine but doesn't use my profile.

我所做的阅读指出,不能同时打开多个浏览器并使用相同的配置文件,因此我确保在运行程序时没有打开任何内容.即使没有打开另一个浏览器,它仍然会挂起 60 秒.

The reading I've done points to not being able to have more than one browser open at a time with the same profile so I made sure nothing was open while I ran the program. It still hangs for 60 seconds even without another browser open.

m_Options = new ChromeOptions();
m_Options.AddArgument("--user-data-dir=C:/Users/Me/AppData/Local/Google/Chrome/User Data");
m_Options.AddArgument("--profile-directory=Default");
m_Options.AddArgument("--disable-extensions");
m_Driver = new ChromeDriver(@"pathtoexe", m_Options);
m_Driver.Navigate().GoToUrl("somesite");

它总是挂在 GoToUrl 上.我不知道还能尝试什么.

It always hangs on the GoToUrl. I'm not sure what else to try.

推荐答案

根据您的代码试验,您试图加载 默认 Chrome 配置文件,这将对所有作为默认 Chrome 配置文件最佳实践可能包含以下任一项:

As per your code trials you were trying to load the Default Chrome Profile which will be against all the best practices as the Default Chrome Profile may contain either of the following:

  • 扩展
  • 书签
  • 浏览历史

因此默认Chrome 配置文件可能不符合您的测试规范,并且可能在加载时引发异常.因此,您应该始终使用自定义 Chrome 配置文件,如下所示.

So the Default Chrome Profile may not be in compliance with you Test Specification and may raise exception while loading. Hence you should always use a customized Chrome Profile as below.

创建打开一个新的Chrome配置文件,您需要按照以下步骤操作:

To create and open a new Chrome Profile you need to follow the following steps :

  • 打开Chrome浏览器,点击侧边菜单并点击设置urlchrome://settings/ 打开.
  • 人员部分,点击弹出窗口的管理其他人员.
  • 点击ADD PERSON,提供人名,选择一个图标,保留该项目为此创建一个桌面快捷方式用户选中并点击ADD按钮.
  • 您的新个人资料已创建.
  • 新个人资料的快照SeLeNiUm
  • Open Chrome browser, click on the Side Menu and click on Settings on which the url chrome://settings/ opens up.
  • In People section, click on Manage other people on which a popup comes up.
  • Click on ADD PERSON, provide the person name, select an icon, keep the item Create a desktop shortcut for this user checked and click on ADD button.
  • Your new profile gets created.
  • Snapshot of a new profile SeLeNiUm

  • 现在桌面图标将创建为 SeLeNiUm - Chrome
  • 从桌面图标的属性 SeLeNiUm - Chrome 中获取配置文件目录的名称.例如--profile-directory="Profile 2"
  • Now a desktop icon will be created as SeLeNiUm - Chrome
  • From the properties of the desktop icon SeLeNiUm - Chrome get the name of the profile directory. e.g. --profile-directory="Profile 2"

  • 获取系统中profile-directory的绝对路径如下:

C:\Users\Thranor\AppData\Local\Google\Chrome\User Data\Profile 2

  • 现在将 profile-directory 的值通过 ChromeOptions 的实例与 AddArgument 方法和键 user 一起传递-data-dir 如下:

  • Now pass the value of profile-directory through an instance of ChromeOptions with AddArgument method along with key user-data-dir as follows :

    m_Options = new ChromeOptions();
    m_Options.AddArgument("--user-data-dir=C:/Users/Me/AppData/Local/Google/Chrome/User Data/Profile 2");
    m_Options.AddArgument("--disable-extensions");
    m_Driver = new ChromeDriver(@"pathtoexe", m_Options);
    m_Driver.Navigate().GoToUrl("somesite");
    

  • 执行你的Test

    这篇关于如何通过 Selenium 的 --user-data-dir 参数打开 Chrome 配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆