c# - 让 Watin session 保持事件状态

标签 c# watin

我正在尝试使用 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/

相关文章:

c# - A 类 DLL 不能转换为 B 类 DLL。 Type A originates.. from in the context LoadFrom

c# - WatIn 多线程

c# - WatiN RunScript 在 FireFox 中失败

c# - 命名空间 'AzureAD' 中不存在类型或命名空间名称 'Microsoft.AspNetCore.Authentication'

javascript - 鼠标中键单击链接仅打开新选项卡 ASP C#

c# - 使用 watin 打开新的资源管理器窗口时鼠标指针跳到左上角

c# - 瓦廷评估问题

nunit - 如何从 teamcity 以管理员身份运行 Visual Studio 或 nunit

c# - 防止使用 new() 构造对象

c# - 认证2.0 |水疗 | id_token如何伪装成access_token来访问受限制的网络资源?