简体   繁体   English

WebBrowser无法正常工作

[英]WebBrowser isn't working

I have this code: 我有以下代码:

This is a button press: 这是一个按钮按下:

int part = 0;
web.Navigate(loginURL.Text + "/auth/login");
wait.Enabled = true;

This is a timer "wait", with interval set to 6000: 这是一个计时器“等待”,间隔设置为6000:

if (part == 0)
            {
                part = 2;
                web.Document.GetElementById("idLoginUserName").SetAttribute("value", user);
                web.Document.GetElementById("idLoginPassword").SetAttribute("value", pass);
                web.Document.GetElementById("idLoginBtn").InvokeMember("click");
            }
            if (part == 2)
            {
                web.Navigate(fullURL.Text);
                part = 3;
            }
            if (part == 3)
            {
                web.Document.GetElementById("title").SetAttribute("value", title.Text);
            }
            if (part == 4)
            {
                web.Navigate("www.vbulletin.com/forum/auth/logout");
                part = 5;
            }
            if (part == 5)
            {
                part = 0;
                web.Navigate(loginURL.Text + "/auth/login");
            }

The button press works fine, however: 按下按钮效果很好,但是:

 web.Document.GetElementById("idLoginUserName").SetAttribute("value", user);
                    web.Document.GetElementById("idLoginPassword").SetAttribute("value", pass);
                    web.Document.GetElementById("idLoginBtn").InvokeMember("click");

does nothing. 什么也没做。 The text isn't changed, the button isn't clicked, etc. I've checked and double checked the IDs and it's right. 文本未更改,按钮未单击,依此类推。我已经检查并仔细检查了ID,这是正确的。

The loginURL.Text is this loginURL.Text是这个

My guess you try to access the document before navigation is completed, this works for me... 我猜您尝试在导航完成之前访问文档 ,这对我有用...

web.DocumentCompleted += (s, e) =>
{
    web.Document.GetElementById("idLoginUserName").SetAttribute("value", user);
    web.Document.GetElementById("idLoginPassword").SetAttribute("value", pass);
    web.Document.GetElementById("idLoginBtn").InvokeMember("click");
};
web.Navigate("http://www.vbulletin.com/forum/auth/login");

You are not obliged to do everything using C#. 您没有义务使用C#进行所有操作。 If you treat the embedded web page as a black box fro the point of view of your app, you can give it internal behaviours using scripts, and jQuery makes this sort of thing short, sweet and very legible for those who come after, with the added benefit of not polluting application logic with functionally trivial but frequently complex UI behaviour code. 如果您从应用程序的角度将嵌入式网页视为黑匣子,则可以使用脚本为其提供内部行为,而jQuery使这种事情变得简短,甜美且清晰易懂,增加的好处是不会在功能上琐碎但经常复杂的UI行为代码污染应用程序逻辑。

$(function(){
  $("#idLoginUserName").val(user);
  $("#idLoginPassword").val(password);
  $("#idLoginBtn").click();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM