简体   繁体   English

如何在VB.NET中的网站上检查文本?

[英]How can I check for text on a site in VB.NET?

I am trying to create a simple program that logs in to a site using a WebBrowser, and it does it fine, but I want it to check if it actually logs in (correct details have been put in) and return the result to the program, I figured it would be possible to search for text on the page after submitting the login to see if it was successful or if it failed. 我正在尝试创建一个简单的程序,该程序使用WebBrowser登录到站点,并且效果很好,但是我希望它检查它是否实际登录(已输入正确的详细信息)并将结果返回给该程序,我认为提交登录后可以在页面上搜索文本以查看登录是否成功。 How would I search for text on the page? 如何在页面上搜索文字? My current code is this: 我当前的代码是这样的:

    Status.Text = "Validating details..."

    WebBrowser1.Navigate("http://www.site.com/login")
    wait(6000)

    WebBrowser1.Document.GetElementById("username").SetAttribute("value", TextBox1.Text)
    WebBrowser1.Document.GetElementById("password").SetAttribute("value", TextBox2.Text)

    WebBrowser1.Document.GetElementById("login").InvokeMember("click")

You can use the All HtmlElementCollection of Document property to get the inner text or inner html of each html element in loaded document. 您可以使用Document的All HtmlElementCollection属性来获取已加载文档中每个html元素的内部文本或内部html。 Here's a little test that I wrote : 这是我写的一个小测试:

public void Test()
    {
        var browser = new WebBrowser();
        var handle = new AutoResetEvent(false);
        browser.DocumentCompleted += (sender, args) => { 
            foreach (HtmlElement element in browser.Document.All) 
                Console.WriteLine(element.InnerHtml);
            handle.Set();
            };
        browser.Navigate("http://www.google.com");
        handle.WaitOne();

    }

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

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