如何通过GetElementByClass选择一个类,点击它programmically [英] How to select a class by GetElementByClass and click on it programmically

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

问题描述

我一直在尝试使用此code阅读HTML / AJAX知道GetElementByClass由类元素不在webBrowser.Document一个选项。我似乎无法得到一个返回值,然后调用成员。是否有变通方法吗?

参考: <一href="http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/7b14a98d-1c81-4ed3-9a1b-f829dbd5e1aa"相对=nofollow>获取HTML元素由类名

例如:

 &LT;跨度类=榜样&GT;(&LT; A HREF =htt​​p://www.test.com/folder/remote/api?=test的onclick = 返回do_ajax('popup_fodder,远程/ API =测试?',1,1,0,0);返回false;阶级=榜样&GT;测试&LT; / A&GT;)&LT; / SPAN&GT;
 

例如code:

  HtmlElementCollection theElementCollection =默认(HtmlElementCollection);
   theElementCollection = webBrowser1.Document.GetElementsByTagName(跨越);
   的foreach(的HtmlElement curElement在theElementCollection)
   {
        //如果curElement.GetAttribute(类)。的ToString =榜样这是行不通的。
        //这应该是周围的工作。
        如果(curElement.OuterHtml.Contains(榜样))
        {
            的MessageBox.show(curElement.GetAttribute(的InnerText)); //甚至不火。
            // InvokeMember(测试)课后发现。
        }
    }
 

解决方案

我承认它不是很直观,但你需要使用的getAttribute(类名),而不是的getAttribute(类)

  HtmlElementCollection theElementCollection =默认(HtmlElementCollection);
   theElementCollection = webBrowser1.Document.GetElementsByTagName(跨越);
   的foreach(的HtmlElement curElement在theElementCollection)
   {
        //如果curElement.GetAttribute(类名)。的ToString =榜样这是行不通的。
        //这应该是周围的工作。
        如果(curElement.GetAttribute(类名)。的ToString =榜样)
        {
            的MessageBox.show(curElement.GetAttribute(的InnerText)); //甚至不火。
            // InvokeMember(测试)课后发现。
        }
    }
 

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?

References: Getting HTMLElements by Class Name

Example:

<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>

Example code:

   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 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"  It doesn't work.  
        // This should be the work around.
        if (curElement.GetAttribute("classname").ToString = "example")
        {
            MessageBox.Show(curElement.GetAttribute("InnerText")); // Doesn't even fire.
            // InvokeMember(test) after class is found.
        }
    }

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

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