我正在尝试使用 Watin 登录网站,并且我想保留 session ,这样我就不会每次都保持登录。
但是,每次我调用 LogIn 行 if (browser.ContainsText(isLoggedInClass))
时都找不到我在打开的浏览器中可以看到的内容。
我添加了行 string checkHtmltoSeeIfClassExists = browser.Body.Parent.OuterHtml;
来实际检查字符串是否包含在 html 中。
我有点卡住了,因为似乎 Watin 并没有坚持 session ?有谁知道出了什么问题吗?
到目前为止我的代码如下
using WatiN.Core;
namespace ProjectXYZ
{
class Navigate
{
private IE browser;
public void LogIn()
{
const string isLoggedInClass = "gwt-Hyperlink optionLink optionLink_myAccount";
if (browser == null)
browser = new IE("https://www.somewebsite.com");
string checkHtmltoSeeIfClassExists = browser.Body.Parent.OuterHtml;
if (browser.ContainsText(isLoggedInClass))
{
string test = "class found!";
}
else
{
browser.TextField(Find.ByName("username")).Value = xx.un;
browser.TextField(Find.ByName("userpass")).Value = xx.pw;
browser.Button(Find.ByTitle("Login")).Click();
}
}
}
}
最佳答案
您想要的机制是 AttachTo<>功能内置于 WatiN 框架中。由于 WatiN 只支持 single-threaded apartment state ,您必须手动管理您的 session 。
上面的链接有一个使用“AttachTo<>”的代码示例,但为了彻底起见,我还在下面提供了一个代码示例。
var IE = IE.AttachTo<IE>(Find.ByUrl(someURLHere));
关于c# - 让 Watin session 保持事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19568046/