WatiN:运行代码时,文本字段没有自动化

发布于 2024-12-06 08:38:16 字数 1056 浏览 0 评论 0原文

WatiN.Core.IE 窗口 = new WatiN.Core.IE();

            // Frames



            // Model

            TextField txt_txtName = window.TextField(Find.ByName("txtName"));

            TextField txt_txtPassword = window.TextField(Find.ByName("txtPassword"));

            Button btn_btnLogin = window.Button(Find.ByName("btnLogin"));



            // Code

            window.GoTo("http://134.554.444.55/asdfgfghh/");

            txt_txtName.TypeText("fghfjghm");

            txt_txtPassword.TypeText("gfhgjfgh");

            btn_btnLogin.Click();

        }

只有 window.GoTo("http://134.554.444.55/asdfgfghh/");代码有效,其余部分什么都不做,

当我使用 catch 块时,它会抛出异常,因为

找不到 INPUT(隐藏)或 INPUT(密码)或 INPUT(文本)或 INPUT(文本区域)或 TEXTAREA 元素标记匹配条件:属性“name”等于“http://134.554.444.55/asdfgfghh/”处的“txtName”(内部异常:无法转换 COM 对象将“System.__ComObject”键入到接口类型“mshtml.IHTMLElement”。此操作失败,因为对 IID 为“{3050F1FF-98B5-11CF-BB82-00AA00BDCE0B}”的接口的 COM 组件上的 QueryInterface 调用由于以下错误而失败。 :加载类型库/DLL 时出错(HRESULT 异常:0x80029C4A) (TYPE_E_CANTLOADLIBRARY))。)

WatiN.Core.IE window = new WatiN.Core.IE();

            // Frames



            // Model

            TextField txt_txtName = window.TextField(Find.ByName("txtName"));

            TextField txt_txtPassword = window.TextField(Find.ByName("txtPassword"));

            Button btn_btnLogin = window.Button(Find.ByName("btnLogin"));



            // Code

            window.GoTo("http://134.554.444.55/asdfgfghh/");

            txt_txtName.TypeText("fghfjghm");

            txt_txtPassword.TypeText("gfhgjfgh");

            btn_btnLogin.Click();

        }

only the window.GoTo("http://134.554.444.55/asdfgfghh/"); code works and the rest are doing nothing,

When I am using a catch block it throws exception as

Could not find INPUT (hidden) or INPUT (password) or INPUT (text) or INPUT (textarea) or TEXTAREA element tag matching criteria: Attribute 'name' equals 'txtName' at "http://134.554.444.55/asdfgfghh/ (inner exception: Unable to cast COM object of type 'System.__ComObject' to interface type 'mshtml.IHTMLElement'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3050F1FF-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following error: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

獨角戲 2024-12-13 08:38:16

我的答案与 Pavlo 的答案非常相似,但我必须建议使用 Page,它是 Pavlo 所描述的 Model 的内置支持。

创建一个类 MyPage

public class MyPage : WatiN.Core.Page
{
    public TextField NameField
    {
        get { return Document.TextField(Find.ByName("txtName")); }
    }

    public TextField PasswordField
    {
        get { return Document.TextField(Find.ByName("txtPassword")); }
    }

    public TextField LoginButton
    {
        get { return Document.Button(Find.ByName("btnLogin")); }
    }
}

然后你可以调用 .Page 方法。

using(var browser = new IE("http://134.554.444.55/asdfgfghh/"))
{
    var page = browser.Page<MyClass>();
    page.NameField.TypeText("name field");
    page.PasswordField.TypeText("password field");
    page.LoginButton.Click();
}

My answer is very similar with Pavlo's one, but I must advice to use Page which is a built-in suport for Model as described by Pavlo.

Create a class MyPage

public class MyPage : WatiN.Core.Page
{
    public TextField NameField
    {
        get { return Document.TextField(Find.ByName("txtName")); }
    }

    public TextField PasswordField
    {
        get { return Document.TextField(Find.ByName("txtPassword")); }
    }

    public TextField LoginButton
    {
        get { return Document.Button(Find.ByName("btnLogin")); }
    }
}

Then you can just call .Page<MyClass> method.

using(var browser = new IE("http://134.554.444.55/asdfgfghh/"))
{
    var page = browser.Page<MyClass>();
    page.NameField.TypeText("name field");
    page.PasswordField.TypeText("password field");
    page.LoginButton.Click();
}
水溶 2024-12-13 08:38:16

当您调用 Button、TextField 或其他不会创建映射的内容时,它实际上会在页面上搜索控件。如果页面尚未打开,则控件不存在。

您可以创建在您请求时找到控制权的属性。因此,您将特定模型定义为具有适当属性的类。

public TextField txt_txtName
{
    get
    {
        return window.TextField(Find.ByName("txtName"));
    }
}

已添加:如果创建属性不适合您,请使用以下命令:

var model = new
{
    txt_txtName = new Func<TextField>(() => window.TextField(Find.ByName("txtName"))),
    txt_txtPassword = new Func<TextField>(() => window.TextField(Find.ByName("txtPassword"))),
    btn_btnLogin = new Func<Button>(() => window.Button(Find.ByName("btnLogin")))
};

window.GoTo("http://134.554.444.55/asdfgfghh/");

model.txt_txtName().TypeText("fghfjghm");

model.txt_txtPassword().TypeText("gfhgjfgh");

model.btn_btnLogin().Click();

When you call Button, TextField or whatever it does not create mapping it actually searches for control on page. And if the page is not opened yet than control does not exist.

You can create properties that will find control when you request it. So you define a particular model as class with appropriate properties.

public TextField txt_txtName
{
    get
    {
        return window.TextField(Find.ByName("txtName"));
    }
}

Added: If creating properties does not work for you, then use this:

var model = new
{
    txt_txtName = new Func<TextField>(() => window.TextField(Find.ByName("txtName"))),
    txt_txtPassword = new Func<TextField>(() => window.TextField(Find.ByName("txtPassword"))),
    btn_btnLogin = new Func<Button>(() => window.Button(Find.ByName("btnLogin")))
};

window.GoTo("http://134.554.444.55/asdfgfghh/");

model.txt_txtName().TypeText("fghfjghm");

model.txt_txtPassword().TypeText("gfhgjfgh");

model.btn_btnLogin().Click();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文