c# - 无法使用 Selenium c# 保存 Whatsapp web 的会话
问题描述
我想保存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;
}
}
我所说的问题再次出现二维码,我只想在使用谷歌浏览器版本 74、网络驱动程序 v 3.141.0 后扫描二维码。
解决方案
请检查配置文件文件夹是否正确。这里的一个旧线程提到您需要将 \Default 添加到配置文件路径。
您是否尝试添加此内容以查看是否有帮助
options.addArguments("chrome.switches", "--disable-extensions")
推荐阅读
- c# - Blazor 组件的模板页面
- cmake - 在 CMake 中一次编译和链接库
- python - 如何手动更改条件
- odata - odata 仅从扩展的子记录集中获取第一条记录?
- javascript - JavaScript:如何在继续执行之前等待相机访问?
- javascript - 为什么 React 中的 JavaScript 方法是未定义的?
- xamarin.forms - ipa 和 apk 包中缺少文件
- liquibase - liquibase:我可以通过引用先前的变更集 id 来回滚删除表吗?
- jarsigner - Jar 签名者:无法打开 jar 文件
- c++ - 埃拉托色尼筛。不确定如何准确实施