2014-07-15 62 views
0

需要从没有ID或名称的div标签的子元素调用onclick方法。在我的网站上有很多div标签。我想遍历具有类名称的特定div标签,并获取其子元素标签,该标签也具有id并调用其中包含的onclick方法。 HTML:如何调用div标签中的onclick事件子元素

新事件

我使用的web浏览器控件在C#中,但不能能够访问一个标签,并调用该方法,因为很多div标签都在那里。 PS没有htmlagilitypack。它是一个安全的环境,我无法下载这些包。

回答

0

如果您不想使用HtmlAgilityPack,则可以使用C#XPath API。它的功能相同。除了正则表达式,这可能是最简单的方法。

0

如果子元素“确实有ID”,那么这个ID应该是HTML文档中是唯一的,然后通过C#代码子元素“点击”可以实现为以下几点:

 var doc = webBrowser1.Document; 
     string childTagId = "{{type your child tag id here}}"; 

     doc.GetElementById(childTagId).InvokeMember("click"); 

但如果孩子标签的ID在HTML文档中不是唯一的,并且DIV的类名是唯一的(根据您的描述进行假设),那么C#代码将变为:

 var doc = webBrowser1.Document; 
     string childTagId = "{{type your child tag id here}}"; 
     string className = "{{type your test className here}}"; 
     string childTagName = "{{type your child tag name here}}"; 

     doc.GetElementsByTagName("div").OfType<System.Windows.Forms.HtmlElement>() 
       .Where(x => x.GetAttribute("className") == className).First() 
       .All.OfType<System.Windows.Forms.HtmlElement>() 
       .Where(x => string.Compare(x.TagName, childTagName, true) == 0 && 
          x.GetAttribute("id") == childTagId) 
       .First() 
       .InvokeMember("click"); 
相关问题