简体   繁体   English

如何使用所需的innerhtml访问span?

[英]How to access span with needed innerhtml?

Say my WebBrowser1 downloaded a page that has a following line: 假设我的WebBrowser1下载了一个包含以下行的页面:

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

It is inside of a td tag, inside of big table, inside of iFrame, inside of div etc. 它在td标签内部,大表内部,iFrame内部,div内部等。

How to I click this thing using c# ? 我如何使用C#单击此东西?

I need to do something following: 我需要执行以下操作:

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

but for some reason it does not work this way, so I'm thinking if it's possible to check the innerHTML of the span, or innerText? 但由于某种原因,它不能以这种方式工作,所以我在考虑是否可以检查span的innerHTML或innerText?

I tried both: 我都尝试过:

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

And I failed both times. 我两次都失败了。

Update: 更新:
I just noticed that the line is actually like this: 我只是注意到该行实际上是这样的:

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

So I wrote a simple function: 所以我写了一个简单的函数:

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

And what I've seen is that this link (and span) does not show up in my log. 我所看到的是此链接(和跨度)未显示在我的日志中。 In other words, getElementsByTagName("span") and getElementsByTagName("a") doesn't see the item. 换句话说,getElementsByTagName(“ span”)和getElementsByTagName(“ a”)看不到该项目。 My guess is that it is because of iFrame. 我的猜测是因为iFrame。 Do you have any thoughts about this? 您对此有什么想法吗?

another solution using no js (because you don't own the "page") since it is inside an iframe then you should search within that iframe 另一个不使用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);
    }
}

Unless I am missing something... 除非我错过了什么...

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

Then when you want to change it... 然后,当您要更改它时...

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

you can make it simpler by using JavaScript and invoking it from c# whenever you need to. 您可以通过使用JavaScript并在需要时从c#调用它来简化它。

WebBrowser1 .Document .InvokeScript ("functionName")

javascript: 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
}

If you set up your span as an HTML server control: 如果将跨度设置为HTML服务器控件:

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

Then you can register an event handler on page load: 然后,您可以在页面加载时注册事件处理程序:

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

Another way to do it is using Page Methods which would call a page C# method using AJAX. 另一种方法是使用页面方法,该方法将使用AJAX调用页面C#方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM