用 Selenium c# 无法保存 Whatsapp web 的会话

4

我想保存whatsapp web的会话,这样每次打开whatsapp web时就不需要扫描QR码了。 我使用:

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

但是qr-code再次出现了。 以下是第一次打开WhatsApp Web并扫描QR码并将其保存到文件夹的方法:
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;
    }
}

这是恢复会话的方法:

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;
    }
}

问题如我所说,二维码再次出现,我希望只扫描一次二维码。我使用的是 Google Chrome 版本 74 和 Web Driver v3.141.0。

1个回答

2

请检查配置文件夹是否正确。一篇旧帖子在这里提到您需要在配置路径中添加\Default。

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

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

我使用了options.addArguments("chrome.switches", "--disable-extensions"),它运行良好。非常感谢。 - Fath Bakri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接