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

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

问题描述

我正在尝试使用我现有的帐户和配置文件中的设置来为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 浏览器,单击侧边菜单,然后单击 url Settings >打开.
  • 在"<人>人"部分中,单击出现弹出窗口的"管理其他人" .
  • 点击添加人,提供人名,选择一个图标,并保留为此项目创建桌面快捷方式用户选中并单击添加按钮.
  • 您的新个人资料已创建.
  • 新个人资料的快照 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 =个人资料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 的绝对路径,如下所示:

  • Get the absolute path of the profile-directory in your system as follows :

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

  • 现在使用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天全站免登陆