Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用WebBrowser获取用JavaScript编写的网页中的超链接_C#_Javascript_Html_Hyperlink_Webbrowser Control - Fatal编程技术网

C# 如何使用WebBrowser获取用JavaScript编写的网页中的超链接

C# 如何使用WebBrowser获取用JavaScript编写的网页中的超链接,c#,javascript,html,hyperlink,webbrowser-control,C#,Javascript,Html,Hyperlink,Webbrowser Control,我使用了上面的代码,可以获得网页的整个HTML文档,但我真正想要的是在HTML文档中获得超链接url(url为(“a href”)) 我使用: webBrowser1.Navigate(myurl); HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("HTML"); foreach (HtmlElement link in links) { MessageBox.Show(link.InnerHtm

我使用了上面的代码,可以获得网页的整个HTML文档,但我真正想要的是在HTML文档中获得超链接url(url为(“a href”))

我使用:

webBrowser1.Navigate(myurl); 
HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("HTML");
foreach (HtmlElement link in links)
{
MessageBox.Show(link.InnerHtml);         
}
但它返回null

有人能帮我解决这个问题吗?不是使用正则表达式,而是使用简单函数之类的东西

带有JavaScript的HTML代码如下所示:

MessageBox.Show(link.GetAttribute("href"));

给我测试一下

这几乎是一个完全的猜测,但是:

  • 你很可能要等到文档真的出现 在抓取内容之前加载
  • 你需要抓住正确的方向 内容(“A”标签)
顺便说一下,我想下面的代码可能会起作用:

首先,设置事件处理程序:

<HTML>
<HEAD>
<TITLE>Test for me</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8" />
<SCRIPT LANGUAGE="JavaScript">
<!--


function window::onload()
{

 result.innerHTML = 
      "<br><center><font size=+1><a href='MyMain.aspx' target='_parent'>Back to My Main        Page</a></font><br>"
    + "<font size=+2><b><a href='http://MySub.asp'>Launch My Application</a></b></font></center>";     
}

-->
</SCRIPT>
</HEAD>

<BODY>
<div id="result" />
</BODY>
</HTML>
在其他地方,定义处理程序(以及需要执行的操作)

最后:

private void ProcessDocument(object sender,
    WebBrowserDocumentCompletedEventArgs e)
{

    var webBrowser1 = (WebBrowser)sender;

    HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("A");
    foreach (HtmlElement link in links)
    {
        MessageBox.Show(link.GetAttribute("href"));         
    }

}
问题是,msdn文档没有说明在文档“完全加载”之前必须发生什么

编辑:我终于在LinqPad中尝试了它,它看起来并没有公开任何与“窗口加载”事件相关的内容,至少没有直接公开。我敢打赌DocumentCompleted事件的触发更像是一个“DOMReady”事件。下面是一个小技巧,但它出现在第三次调用DocumentTitleChange事件时,它捕获了href的内容。请注意,第三次调用它的原因是我有javascript更改了标题

您的html所在位置:

void Main()
{
    WebBrowser webBrowser1 = new WebBrowser();
    webBrowser1.DocumentTitleChanged +=
        new EventHandler(ProcessDocument);
    webBrowser1.Navigate("http://localhost/test/test.html"); 

    Console.ReadLine();
}

// Define other methods and classes here

private void ProcessDocument(object sender,
    EventArgs e)
{

    var webBrowser1 = (WebBrowser)sender;
    Console.WriteLine("ProcessDocument BEGIN");
    HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("A");
    foreach (HtmlElement link in links)
    {
        Console.WriteLine(link.GetAttribute("href"));         
    }
    Console.WriteLine("ProcessDocument END");
    Console.Out.Flush();

}

给我测试一下

我已编辑了您的标题。请参阅“”,其中的共识是“不,他们不应该”。窗口加载函数真的是
函数window::onload()
?那不是javascript。。。我不知道那是什么谢谢你JayC。我会试试你的方法。:)
webBrowser1.Navigate(myurl); 
void Main()
{
    WebBrowser webBrowser1 = new WebBrowser();
    webBrowser1.DocumentTitleChanged +=
        new EventHandler(ProcessDocument);
    webBrowser1.Navigate("http://localhost/test/test.html"); 

    Console.ReadLine();
}

// Define other methods and classes here

private void ProcessDocument(object sender,
    EventArgs e)
{

    var webBrowser1 = (WebBrowser)sender;
    Console.WriteLine("ProcessDocument BEGIN");
    HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("A");
    foreach (HtmlElement link in links)
    {
        Console.WriteLine(link.GetAttribute("href"));         
    }
    Console.WriteLine("ProcessDocument END");
    Console.Out.Flush();

}
<HTML>
<HEAD>
<TITLE>Test for me</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8" />
<SCRIPT LANGUAGE="JavaScript">
<!--


function foo()
{
  var result = document.getElementById('result');
 result.innerHTML = 
      "<br><center><font size=+1><a href='MyMain.aspx' target='_parent'>Back to My Main        Page</a></font><br>"
    + "<font size=+2><b><a href='http://MySub.asp'>Launch My Application</a></b></font></center>";
  document.title += "Hack..Aacklgahala, ribbit";    
}

-->
</SCRIPT>
</HEAD>

<BODY onload="foo()">
<a href="http://google.com">bar</a>
<div id="result" />
</BODY>
</HTML>