【问题标题】:How to access span with needed innerhtml?如何使用所需的 innerhtml 访问 span?
【发布时间】:2011-11-03 19:11:36
【问题描述】:

假设我的 WebBrowser1 下载了一个包含以下行的页面:

<span customattr="hamolulu">hamolulu</span>

它位于 td 标签内、大表内、iFrame 内、div 内等。

如何使用 c# 点击这个东西?

我需要做以下事情:

int i = 0;
for (i = 0; i <= 500; i++)
{
    if (webBrowser1.Document.GetElementsByTagName("span")[i].GetAttribute("customattr") == "hamolulu")
    {
        webBrowser1.Document.GetElementsByTagName("span")[i].InvokeMember("click");
        break;
    }//end if
}// end for

但由于某种原因它不能以这种方式工作,所以我在想是否可以检查 span 的 innerHTML 或 innerText?

我都试过了:

webBrowser1.Document.GetElementsByTagName("span").InnerHTML == "hamolulu"
webBrowser1.Document.GetElementsByTagName("span").InnerText == "hamolulu"

我两次都失败了。

更新:
我刚刚注意到这条线实际上是这样的:

<span customattr="hamolulu"><a>hamolulu</a></span>

于是我写了一个简单的函数:

int i = 0;
for (i = 0; i <= webBrowser1.Document.GetElementsByTagName("a").Count - 1; i++)
{
  log(i.ToString()+ " : " +webBrowser1.Document.GetElementsByTagName("a")[i].InnerHtml);
} //log(string) is a custom function that saves all strings to a file log.txt

我所看到的是这个链接(和跨度)没有出现在我的日志中。 换句话说,getElementsByTagName("span") 和 getElementsByTagName("a") 看不到该项目。我的猜测是因为 iFrame。你对此有什么想法吗?

【问题讨论】:

    标签: c# javascript .net html


    【解决方案1】:

    另一种不使用 js 的解决方案(因为您不拥有“页面”) 因为它在 iframe 内,所以您应该在该 iframe 内搜索

    HtmlElementCollection iframes = WebBrowser1.Document.GetElementsByTagName("iframe");
    HtmlElement iframe = iframes(0 /* iframe index */); // 
    
    HtmlElementCollection spans = iframe.Document.GetElementsByTagName("span");
    for (i = 0; i < spans.Count; i++) {
        HtmlElement span = spans(i);
        if (span.GetAttribute("customAttr") == "customAttrValue") {
            string onclick = span.Children(0).GetAttribute("onclick"); //span.Children(0) should return the <a>
            WebBrowser1.Document.InvokeScript(onclick);
        }
    }
    

    【讨论】:

    • 我喜欢这个想法,这就是我刚刚所做的,但是 getElementsByTagName 没有看到所需的 span / a(查看更新)
    • 您能否提供一个指向您正在尝试处理的页面的链接?
    • 我刚刚发现了一个问题。它是 iFrame。当我尝试记录它的 innerhtml 时,它返回为空,尽管萤火虫确实看到了它。我可能会提出一个新问题,因为这个问题太大且令人困惑。
    【解决方案2】:

    除非我遗漏了什么……

    <span id="hamolulu">hamolulu</span>
    

    那么当你想改变它的时候......

    document.getElementById('hamolulu').innerHTML="<h1>Test!</h1>";
    

    【讨论】:

    • 我需要在 c# 中制作它,而不是在 JavaScript 中。不过还是谢谢。
    • 哦,hamolulu 不是 ID - 它同时是自定义属性和 innerhtml,因此 GetElementByID 不起作用。
    • @SerhyiVynohradov,这就是为什么在我的示例中,我将其更改为 id。我不确定你为什么想做你正在做的事情。
    • 因为我不拥有该站点,所以我无法将其更改为 ID,唯一使这个 span 唯一的就是这个属性。
    • 如果是我,我会使用 jQuery:$('select[customattr="hamolulu"]').text("new text");
    【解决方案3】:

    如果您将 span 设置为 HTML 服务器控件:

    <span runat="server" id="myspan" customattribute="customvalue">hello world</span>
    

    然后您可以在页面加载时注册一个事件处理程序:

     protected void Page_Load(object sender, EventArgs e)
        {
            myspan.Attributes["onclick"] = "this.innerText='hamolulu'";
        }
    

    另一种方法是使用页面方法,它会使用 AJAX 调用页面 C# 方法。

    【讨论】:

      【解决方案4】:

      您可以通过使用 JavaScript 并在需要时从 c# 调用它来简化它。

      WebBrowser1 .Document .InvokeScript ("functionName")
      

      javascript:

      function functionName(){
      
        var spans = document.getElementsByTagName('SPAN');
        for (i = 0; i < spans.length; i++)
        {
          var span = spans[i];
          if (span.getAttribute("customattr") == "hamolulu")
          {
              eval(span.getAttribute('onclick')); // .. be careful "span" has no "click()" method. you should use the onlick attribute if available
              break;
          }//end if
        }// end for
      }
      

      【讨论】:

      • 但是我该把这个javascript代码放在哪里呢?如何把它放到我的c#中?作为一个简单的字符串?
      • 尝试使用脚本标签将其添加到页面中
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 2011-11-19
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      相关资源
      最近更新 更多