2013-07-17 67 views
0

我正在尝试获取单击按钮后得到的网页的源代码。如何在代码中模拟网页按钮点击后获取源代码

我可以点击位于网页上的按钮。

webBrowser1.Navigate(url); 
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) 
{ 
    Application.DoEvents(); 
}    
webBrowser1.Document.GetElementById("downloadButton").InvokeMember("click"); 

此后出现一个新窗口。点击后可能会出现这个新窗口的源代码。

回答

0

哈克方法是:

  1. 附加的事件处理程序的按钮的的“onClick”事件。
  2. 然后,一旦事件被触发,请使用Microsoft Internet Controls(SHDocVw)类型库以获取在IE中打开的最后一个URL。
  3. 最后,导航到URL,一旦加载文档,从webBrowser1.DocumentText属性获取文档的来源。

在项目中,添加到Microsoft Internet控制类型库的引用(你会发现它在COM标签)。添加在你的文件的顶部:

using SHDocVw; 

代码:

webBrowser1.Navigate(url); 
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) 
{ 
    Application.DoEvents(); 
} 

// assign the button to a variable 
var button = webBrowser1.Document.GetElementById("downloadButton"); 

// attach an event handler for the 'onclick' event of the button 
button.AttachEventHandler("onclick", (a, b) => 
{ 
    // use the Microsoft Internet Controls COM library 
    var shellWindows = new SHDocVw.ShellWindows(); 

    // get the location of the last window in the collection 
    var newLocation = shellWindows.Cast<SHDocVw.InternetExplorer>() 
     .Last().LocationURL; 

    // navigate to the newLocation 
    webBrowser1.Navigate(newLocation); 
    while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) 
    { 
     Application.DoEvents(); 
    } 

    // get the document's source 
    var source = webBrowser1.DocumentText; 
}); 

button.InvokeMember("click"); 
+0

我通过上面的代码运行了一个脚本错误。说道:!此页面上的脚本发生错误。 – Manish