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

C# 如何使用所需的innerhtml访问span?,c#,javascript,.net,html,C#,Javascript,.net,Html,假设my WebBrowser1下载了一个包含以下行的页面: <span customattr="hamolulu">hamolulu</span> 我两次都失败了 更新: 我刚刚注意到这条线实际上是这样的: <span customattr="hamolulu"><a>hamolulu</a></span> 哈莫鲁鲁 所以我写了一个简单的函数: int i = 0; for (i = 0; i <= webBr

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

<span customattr="hamolulu">hamolulu</span>
我两次都失败了

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

<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
inti=0;

对于(i=0;i除非我遗漏了什么

<span id="hamolulu">hamolulu</span>
哈莫鲁鲁
然后当你想改变它的时候

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

您可以通过使用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
}
函数functionName(){
var SPAN=document.getElementsByTagName('SPAN');
对于(i=0;i
如果将span设置为HTML服务器控件:

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

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

另一种不使用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);
    }
}
HtmlElementCollection iframes=WebBrowser1.Document.GetElementsByTagName(“iframe”);
HtmlElement iframe=iframes(0/*iframe索引*/);//
HtmlElementCollection span=iframe.Document.GetElementsByTagName(“span”);
对于(i=0;i
我需要用c#,而不是JavaScript。不过还是要谢谢你。哦,hamolulu不是一个ID-它同时是自定义属性和innerhtml,所以GetElementByID不起作用。@SerhyiVynohradov,这就是为什么在我的例子中,我把它改成了
ID
。我不确定你为什么要这么做。因为我不拥有这个网站,因此,我无法将其更改为ID,唯一使此跨度唯一的是此属性。如果是我,我将使用jQuery:
$('select[customattr=“hamolulu”]')。text(“新文本”)
但是我应该把这个javascript代码放在哪里?如何把它作为一个简单的字符串放进我的c#?试着用一个脚本标记将它添加到页面上我喜欢这个想法,这就是我刚才做的,但是getElementsByTagName没有看到所需的span/a(签出更新)你能提供一个链接到你试图使用的页面吗?我刚刚发现一个问题。它是iFrame。当我尝试记录它的innerhtml时,它返回空的,尽管firebug确实看到了它。我可能会提出一个新问题,因为这个问题变得太大和混乱。
<span runat="server" id="myspan" customattribute="customvalue">hello world</span>
 protected void Page_Load(object sender, EventArgs e)
    {
        myspan.Attributes["onclick"] = "this.innerText='hamolulu'";
    }
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);
    }
}