c# - 无法使用 Selenium c# 保存 Whatsapp web 的 session

标签 c# selenium selenium-chromedriver whatsapp

我想保存 whatsapp web 的 session ,这样我就不必每次打开 whatsapp web 时都扫描二维码。我使用:

options.AddArgument("--user-data-dir=" + FolderPathToStoreSession)

但是二维码又出现了。 下面是第一次打开whatsapp web 扫描二维码并保存到文件夹的方法:

public static int OpenNewChrome(
        string Website,
        int TimeToWaitInMinutes,
        string FolderPathToStoreSession)
{
    ChromeOptions options = null;
    ChromeDriver driver = null;
    try
    {
        //chrome process id
        int ProcessId = -1;
        //time to wait until open chrome
        var TimeToWait = TimeSpan.FromMinutes(TimeToWaitInMinutes);
        ChromeDriverService cService = ChromeDriverService.CreateDefaultService();
        //hide dos screen
        cService.HideCommandPromptWindow = true;
        options = new ChromeOptions();
        //session file directory
        options.AddArgument("--user-data-dir=" + FolderPathToStoreSession);
        driver = new ChromeDriver(cService, options, TimeToWait);

        //set process id of chrome
        ProcessId = cService.ProcessId;

        driver.Navigate().GoToUrl(Website);

        FRM_MSG f2 = new FRM_MSG();
        DialogResult r = f2.ShowDLG(" ",
             "Did you successfully finish scan bardcode?",
               FRM_MSG.MSGIcon.Question,
               FRM_MSG.BTNS.Two,
               new string[] { "Yes Finish", "Cannot scan qr-code" });

        if (driver != null)
        {
            driver.Close();
            driver.Quit();
            driver.Dispose();
        }
        if (r == DialogResult.Yes)
            return ProcessId;
        return -1;
    }
    catch (Exception ex)
    {
        if (driver != null)
        {
            driver.Close();
            driver.Quit();
            driver.Dispose();
        }
        driver = null;
        throw ex;
    }
}

这是恢复 session 的方法:

public static int OpenOldChrome(
       string Website,
       int TimeToWaitInMinutes,
       string FolderPathToStoreSession)
{
    ChromeOptions options = null;
    ChromeDriver driver = null;
    try
    {
        //chrome process id
        int ProcessId = -1;
        //time to wait until open chrome
        var TimeToWait = TimeSpan.FromMinutes(TimeToWaitInMinutes);
        ChromeDriverService cService = ChromeDriverService.CreateDefaultService();

        //hide dos screen
        cService.HideCommandPromptWindow = true;

        options = new ChromeOptions();

        //session file directory
        options.AddArgument("--user-data-dir=" + FolderPathToStoreSession);

        driver = new ChromeDriver(cService, options, TimeToWait);

        //set process id of chrome
        ProcessId = cService.ProcessId;

        Thread.Sleep(50000);

        FRM_MSG f2 = new FRM_MSG();
        DialogResult r = f2.ShowDLG(" ",
             "Did you wnat to exit?",
               FRM_MSG.MSGIcon.Question,
               FRM_MSG.BTNS.Two,
               new string[] { "Yes", "No" });

        if (driver != null)
        {
            driver.Close();
            driver.Quit();
            driver.Dispose();
        }
        if (r == DialogResult.Yes)
            return ProcessId;

        return -1;
    }
    catch (Exception ex)
    {
        if (driver != null)
        {
            driver.Close();
            driver.Quit();
            driver.Dispose();
        }
        driver = null;
        throw ex;
    }
}

我说的二维码问题又出现了,我只想扫描一次二维码 我使用谷歌浏览器 74 版,网络驱动程序 v 3.141.0。

最佳答案

请检查配置文件文件夹是否正确。一个旧线程here提到您需要将\Default 添加到配置文件路径。

您是否尝试添加此内容以查看是否有帮助

options.addArguments("chrome.switches", "--disable-extensions")

关于c# - 无法使用 Selenium c# 保存 Whatsapp web 的 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58463761/

相关文章:

C# 析构函数 : the dispose "pattern" and best practises

c# - Rx.Net 消息解析器

python - 使用 Python 获得所需的功能

java - 如何使用 Selenium TestNG 和 Java 转换和匹配背景颜色 RGB(255, 255, 255) 与#fff

python-3.x - 加载资源失败 : the server responded with a status of 429 (Too Many Requests) and 404 (Not Found) with ChromeDriver Chrome through Selenium

c# - 我真的在这里使用任务和异步获得了任何东西吗?

C# 将多个对象保存到文件

python - 我想获取所有图像,但 selenium python 只获取三个图像

selenium - 实现jenkins docker image + chrome + ruby​​未知错误时出现问题:Chrome无法启动:崩溃(Selenium::WebDriver::Error::UnknownError)

java - selenium jre1.7和1.8兼容性冲突