如何通过GetElementByClass选择一个类并以编程方式单击它 [英] How to select a class by GetElementByClass and click on it programmatically

查看:155
本文介绍了如何通过GetElementByClass选择一个类并以编程方式单击它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用此代码按html / ajax中的类读取元素,知道GetByByClass不是webBrowser.Document中的选项。我似乎无法获得返回值,然后调用该成员。

I have been trying to use this code to read the element by class in html/ajax knowing GetElementByClass is not a option in webBrowser.Document. I can't seem to get a return value then invoke the member. Is there a work around for this?

参考文献:
通过类名称获取HTMLElements

示例:

<span class="example">(<a href="http://www.test.com/folder/remote/api?=test" onclick=" return do_ajax('popup_fodder', 'remote/api?=test', 1, 1, 0, 0); return false; " class="example">test</a>)</span>

示例代码:

   HtmlElementCollection theElementCollection = default(HtmlElementCollection);
   theElementCollection = webBrowser1.Document.GetElementsByTagName("span");
   foreach (HtmlElement curElement in theElementCollection)
   {
        //If curElement.GetAttribute("class").ToString = "example"  It doesn't work.  
        // This should be the work around.
        if (curElement.OuterHtml.Contains("example"))
        {
            MessageBox.Show(curElement.GetAttribute("InnerText")); // Doesn't even fire.
            // InvokeMember(test) after class is found.
        }
    }


推荐答案

I承认它不是很直观,但是您需要使用 GetAttribute( className)而不是 GetAttribute( class)

I admit it's not very intuitive but you need to use GetAttribute("className") instead of GetAttribute("class")

HtmlElementCollection theElementCollection = default(HtmlElementCollection);
theElementCollection = webBrowser1.Document.GetElementsByTagName("span");
foreach (HtmlElement curElement in theElementCollection)
{
    if (curElement.GetAttribute("className").ToString() == "example")
    {
        MessageBox.Show(curElement.GetAttribute("InnerText")); // Do something you want
    }
}

这篇关于如何通过GetElementByClass选择一个类并以编程方式单击它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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