【问题标题】:How to load specific google chrome profile using c# & Selenium chrome driver?如何使用 c# 和 Selenium chrome 驱动程序加载特定的 google chrome 配置文件?
【发布时间】:2019-03-25 14:40:22
【问题描述】:

我需要使用 C# 和 Selenium chrome 驱动程序加载我当前的 google chrome 配置文件,其中包含所有扩展程序/cookies/登录名和密码。 请帮助我将正确的代码行放在正确的位置。

我发现了一些人们如何使用 python 做到这一点的信息,但即使在这些线程中,也没有答案将代码放在哪里。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
IWebDriver Browser;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{


IWebElement element;

Browser = new OpenQA.Selenium.Chrome.ChromeDriver();
Browser.Manage().Window.Maximize();
Browser.Navigate().GoToUrl("https://google.com");
Thread.Sleep(3000);

}

private void button2_Click(object sender, EventArgs e)
{
Browser.Quit();
}

我提供的代码成功打开了一个空的 google chrome 配置文件。我需要 selenium chrome 驱动程序才能使用我的 google chrome 配置文件而不是空配置文件打开 google chrome。

【问题讨论】:

    标签: c# selenium


    【解决方案1】:

    欢迎来到 SO。您可以使用 chromeoptions。

    ChromeOptions options = new ChromeOptions();
    options.addArguments("user-data-dir=/path/to/your/custom/profile");
    

    请参阅http://chromedriver.chromium.org/capabilities,了解更多信息。 确保在驱动程序中使用 chrome 选项。

    IWebDriver driver = new ChromeDriver(options);
    

    【讨论】:

    • 在此行之前添加了我个人资料的路径。浏览器 = 新 OpenQA.Selenium.Chrome.ChromeDriver();现在我得到了这个错误:找不到命名空间“ChromeOptions”的类型或名称(可能没有使用指令或程序集的链接)
    • 添加使用; OpenQA.Selenium.Chrome ,现在一切正常,但仍会打开一个空的 google chrome 配置文件。这是我添加到 user-data-dir=C:\Users\George\AppData\Local\Google\Chrome\User Data\Default 的路径 此文件夹由 19 个扩展文件夹组成,所以认为它已正确粘贴出了什么问题?。
    • 去掉最后的Default
    • chrome_options.add_argument("user-data-dir=C:\\Users\\XXXX\\AppData\\Local\\Google\\Chrome\\User Data") 然后在 python 中启动 chrome driver = webdriver.Chrome(chrome_options=chrome_options) 并打开一个空白浏览器,其中包含我正在使用的所有扩展。
    猜你喜欢
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多