如何通过Webbrowser获取Elementbyname。 [英] How Do I Get Elementbyname Via Webbrowser.

查看:459
本文介绍了如何通过Webbrowser获取Elementbyname。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过webbrowser填充输入。当我尝试getelementbyname我得到空值。你能帮我解决这个问题吗?



这是我导航的网站的html代码:

I need to fill inputs via webbrowser. When i try to getelementbyname i am getting null value. Could you help me solve this problem please?

Here is html code of a website which i navigated:

<input onfocus="showInfoBox(this, "Varchar(100) Latin capital letters only (A-Z) and spaces between surnames")" onblur="hideInfoBox()" value="" name="Surname"><input>





这是我的代码:



Here is my code:

public HtmlElementCollection GetElemByName(string name)
{
    if (webBrowser1.Document == null)
        return null;

    HtmlDocument doc = webBrowser1.Document;

    return doc.All.GetElementsByName(name);
}


private void button2_Click(object sender, EventArgs e)
{
    HtmlElementCollection col = GetElemByName("Surname");
    if (col != null && col.Count > 0)
    {
        col[0].SetAttribute("Surname", "My Surname");
    }
}

推荐答案

确保您尝试仅在访问元素时DOM已准备就绪 - 请注意,控件是异步运行的。我不知道你是从控制台还是从Windows窗体中尝试这个,但是apprach在这里突出显示:如何使用没有winform的WebBrowser [ ^ ]。它会起作用,它适用于我的测试。



尽管如此,你最好不要使用WebBrowser控件。如果您需要在浏览器中模拟用户操作,您可以使用更好的工具自动化浏览器(例如IE),例如线束 [ ^ ]或 WatiN [ ^ ],或者您可以完全忘记浏览器并使用< a href =http://www.dotnetperls.com/webc> WebClient [ ^ ]或 HttpWebRequest [ ^ ]在http级别与服务器交互。如果你想做一些批处理或后台工作,这就是方法。你最有可能需要做一些嗅探(使用 Fiddler [ ^ ]例如)查看客户端实际发送和接收的内容,但如果它不是具有随机自动生成ID的ASP.NET Web窗体应用程序,名字,快速得到这个想法并不难。这样你就拥有了所有的控制权并消耗了相当少的资源。
Make sure, that you try to access the elements only when DOM is ready - be aware, that the control is running asycronously. I don't know if you are trying this from console or from windows forms, but the apprach is highlighted here: How Use WebBrowser without winform[^]. It will work, it worked with my test.

Still, you better not use WebBrowser control for that. If you need to mock the user actions in a browser, you can either automate the browser (IE for example) with better tools, like Harness[^] or WatiN[^], or you can forget the browser alltogether and use WebClient[^] or HttpWebRequest[^] to interact with the server on http level. If you want to do some batch or background job, this is the way. You will most likelly need to do some sniffing (with Fiddler[^] for example) to see what's actually sent and received by the client, but if it's not an ASP.NET Web Forms application with random autogenerated ID's and names, it will be not hard to get the idea quickly. This way you have all the control and consume considerably less resources.


static HtmlElementCollection FillInputs(WebBrowser browser)
{
var input = browser.Document.GetElementsByTagName("input");
foreach (HtmlElement element in input)
{
    if (element.GetAttribute("name") == "Surname")
    {
         element.SetAttribute("value", "My Surname");
    }
}
return input;
}




private void button2_Click(object sender, EventArgs e)
{
    var collection = FillInputs(webBrowser1);
}


这篇关于如何通过Webbrowser获取Elementbyname。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆