无法使用Selenium C#保存Whatsapp Web的会话

时间:2019-10-19 12:38:08

标签: c# selenium selenium-chromedriver whatsapp

我想保存whatsapp网站的会话,因此不必每次打开whatsapp网站时都扫描二维码。我使用:

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

,但qr-code再次出现。 这是第一次打开whatsapp网站以扫描二维码并将其保存到文件夹的方法:

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。

1 个答案:

答案 0 :(得分:1)

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

您是否尝试添加它以查看是否有帮助

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