c# - 按值获取html元素

标签 c#

鉴于 Winforms Webbrowser DOM 中的以下 html,我尝试使用 value="type102"

获取第二个元素
    <div class="Input"><input type="radio" name="type" value="type101" 
onclick="setType('type101');"><a href="javaScript:setType('type101');" 
onclick="s_objectID=&quot;javascript:setType('type101');_1&quot;;return this.s_oc?
this.s_oc(e):true">type101</a></div>

    <div class="Input"><input type="radio" name="type" value="type102" 
onclick="setType('type102');" checked="checked"><a href="javaScript:setType('type102');" 
onclick="s_objectID=&quot;javascript:setType('type102');_1&quot;;return this.s_oc?
this.s_oc(e):true">type102</a></div>

我用过 HtmlElement htmlElem = browser.Document.GetElementById(....

HtmlElement htmlElem = browser.Document.All.GetElementsByName(....

之前,但在本例中它们是相同的,因此需要通过值或 href 获取

是否可以在没有外部库的情况下直接获取第二个元素,或者我是否必须获取 GetElementsByName 的集合并遍历它们?

最佳答案

HtmlElementCollection col=  webBrowser1.Document.GetElementsByTagName("input");
HtmlElement wanted;

foreach (HtmlElement item in col)
{
    if (item.GetAttribute("value")=="type102")
    {
        wanted = item;
        break;
    }
}

关于c# - 按值获取html元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12935398/

相关文章:

c# - 如何在 WPF 中将位图渲染到 Canvas 中?

c# - 将文件一次加载到 ASP.NET 应用程序中

c# - 我可以创建一个包装器来拦截对实现特定接口(interface)的对象的所有调用吗?

javascript - ASP MVC AJAX 返回对象并在 JavaScript 中作为关联数组进行访问

c# - await in try/catch/finally 的一个好的解决方案?

c# - 在毛伊岛构建节拍器,但声音播放器不规则并且对计时器不响应。间隔

c# - 注入(inject)依赖项并将其成员之一设置为 `this` 是否有代码味道?

c# - 如何设置 ASP.NET 身份验证属性

c# - ASP.NET MVC 如何将 ModelState 错误转换为 json

c# - 注入(inject)路由注册时,在何处调用 RouteDebugger.RewriteRoutesForTesting()?