【问题标题】:Can't save the session of Whatsapp web with Selenium c#无法使用 Selenium c# 保存 Whatsapp web 的会话
【发布时间】:2020-02-16 05:06:29
【问题描述】:

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

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

qr-code 再次出现。 这是第一次打开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;
    }
}

这里是恢复会话的方法:

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,网络驱动程序 v 3.141.0。

【问题讨论】:

    标签: c# selenium selenium-chromedriver whatsapp


    【解决方案1】:

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

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

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

    【讨论】:

    • 我使用 options.addArguments("chrome.switches", "--disable-extensions") 效果很好 比这么多
    猜你喜欢
    • 2018-11-20
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 2014-03-24
    • 2020-09-27
    • 2014-08-23
    相关资源
    最近更新 更多