如何获取框架的HTML [英] How to get HTML of a frame

查看:79
本文介绍了如何获取框架的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.net论坛上发布了这个问题,但因为被认为是偏离主题而被删除了。现在我把它重新发布在这里,希望有人能够对这个问题有所了解。该查询与对DOM,HTML文档,框架和javascript的编程访问有关。

这是我原来的帖子



我试图通过其网络界面以编程方式模拟手动导航,从登录到重启选项,自动化C#中TP-link路由器的重启过程。路由器的Web界面最初加载在WPF的"WebBrowser"上。
控制,引用为"wbTplink"在代码隐藏中。我使用WPF控件的事实与这篇文章无关。重要的是来自"Microsoft.mshtml"的类型和方法。我试图用于任务的命名空间,但是
到目前为止收效甚微。



Web界面实现:

I posted this question on ASP.net forums but was deleted because it was considered off-topic. Now I repost it here hoping that someone shed some light on the issue. The query has to do with programmatic access to the DOM, HTML documents, frames and javascript.
Here is my original post:

I'm trying to automate the reboot process of a TP-link router in C# by programmatically simulating the manual navigation through its web interface, from logon up to the reboot option. The router's web interface is initially loaded on a WPF's "WebBrowser" control, referenced as "wbTplink" in codebehind. The fact that I used a WPF control is not relevant to this post. What's important are the types and methods from the "Microsoft.mshtml" namespace that I'm trying to use for the task, but with little success so far.

The web interface implements:


  • 表单身份验证
  • 多个框架

  • 很多javascrip

图。 #1显示登录窗口。

图#2显示登录后的登陆页面。



长话短说,我可以成功登录打开并浏览javascript菜单项,直到我到达"重新启动"状态。 。标签  "重新启动"选项卡与其他三个选项卡一起位于窗口右侧的自己的框架上。
我可以重新启动路由器,如图3所示。从那里,我们的想法是以编程方式点击"重新启动"。按钮(id = btn_reboot),或者更好的是,运行"doReboot()"按钮。 javascript函数。问题是我是
无法检索"btn_reboot"选项卡的IHTEMLD文档。按钮和doReboot()javascript函数已定义。无论我用于框架的索引号是什么,我都可以获得与图#2(ref_index = 1)上呈现的内容相对应的文档(ref_index = 1),或者由于来自"mshtml"的非托管代码的异常而没有任何内容。 。当然,我从设置ref_index = 1获得的IHTMLDocument不包含"btn_reboot"。按钮或"doReboot()"按钮函数
只是因为它与wbTplink控件所呈现的内容不对应(图#3)。简单地说,我找不到一种方法来达到"重启"的HTML代码。标签。更糟糕的是,导航到"重新启动"
标签不会引发wbTplink的"LoadCompleted"事件。



因此,考虑到图#3,我的问题就是这样:我需要做什么才能以编程方式获取HTML文档"重启"位于wbTplink窗口右侧框架上的标签?



有关更多上下文,请参阅XAML和C#代码和注释。只粘贴相关的部分:

Fig. #1 shows the logon window.
Fig. #2 shows the landing page after logon.

Long story short, I could successfully log on and navigate through the javascript menu items until I reach the "Reboot" tab.  The "Reboot" tab, together with other three tabs, resides on its own frame on the right-hand side of the window. The closest I could get to rebooting the router is shown in Fig. #3. From there, the idea is to programmatically click the "Reboot" button (id=btn_reboot), or, even better, run the "doReboot()" javascript function. The problem is that I'm unable to retrieve the IHTEMLDocument of the tab where the "btn_reboot" button and the doReboot() javascript function are defined. No matter what index number I use for the frame, I either get the document that corresponds to the content rendered on Fig. #2 (ref_index=1) or just nothing due to exceptions on unmanaged code from "mshtml". Of course, the IHTMLDocument that I get from setting ref_index=1 doesn't contain the "btn_reboot" button or the "doReboot()" function simply because it doesn't correspond to the content that's being rendered by the wbTplink control (Fig. #3). Simply put, I just can't find a way to reach the HTML code of the "Reboot" tab. To make things worse, navigating to the "Reboot" tab doesn't raise wbTplink's "LoadCompleted" event.

So, with Fig. #3 in mind, my question simply is this: What do I have to do to programmatically get the HTML document of the "Reboot" tab that's on the frame to the right-hand side of the wbTplink window?

See the XAML and C# code and comments for more context. Only the relevant pieces are pasted:

<WebBrowser Name="wbTplink" Source="http://192.168.1.2/" Width="600" Height="600" LoadCompleted="wbTplink_LoadCompleted"/>


private void wbTplink_LoadCompleted(object sender, NavigationEventArgs e) { HTMLDocumentClass docClass = wbTplink.Document as HTMLDocumentClass; IHTMLElement usr = docClass.getElementById("txt_usr_name"); usr.innerText = "Administrator"; IHTMLElement pas = docClass.getElementById("txt_password"); pas.innerText = "MyPassword"; IHTMLElement btn = docClass.getElementById("btn_logon"); wbTplink.LoadCompleted -= wbTplink_LoadCompleted; // Raised after logon window is rendered. wbTplink.LoadCompleted += wbTplink_LoadCompleted1; // Raised after "btn.click()" btn.click(); } private void wbTplink_LoadCompleted1(object sender, NavigationEventArgs e) { // Flow continues here ... wbTplink.LoadCompleted -= wbTplink_LoadCompleted1; // This event is never raised. // Not even after the navigate() method finishes rendering the "Reboot.htm" file. See [*] below. wbTplink.LoadCompleted += wbTplink_LoadCompleted2; // BEGIN Fig. #3 // ------------- object ref_index = 0 as object; HTMLDocumentClass docClass = wbTplink.Document as HTMLDocumentClass; IHTMLFramesCollection2 frames = docClass.frames; IHTMLWindow2 currentFrame = (IHTMLWindow2)frames.item(ref ref_index); HTMLDocument document_sub = ((HTMLDocument)currentFrame.document); // This gets the HTML of all the items that comprise the javascript menu on // the frame to the left of the window. IHTMLElement element_sub = (document_sub.getElementById("div_menu")); // GREAT! This works. IHTMLElement maintenance = (document_sub.getElementById("a28")); maintenance.click(); // So far, so good. ref_index = 1 as object; IHTMLWindow2 currentFrame1 = (IHTMLWindow2)frames.item(ref ref_index); // [*] currentFrame1.navigate("http://192.168.1.2/userRpm/Reboot.htm"); // END Fig. #3 // ----------- // No matter what I do below, it is impossible to retrieve the HTML // document of the "Reboot.htm" page. // The following code retrieves the HTML that corresponds to the frame shown in Fig. #2 // even though Fig. #3 is rendered on the wbTplink control. // I want the HTML shown in Fig. #3 so that I have access to btn_reboot or doReboot(). // Trying different values of ref_index doesn't make any difference. HTMLDocument document_sub2 = ((HTMLDocument)currentFrame1.document); IHTMLElement element_sub2 = (document_sub2.getElementById("div_main_pane")); MessageBox.Show(element_sub2.innerHTML); // This works only if "ref_index = 1" } private void wbTplink_LoadCompleted2(object sender, NavigationEventArgs e) { // This event handler is never called because navigating through the // javascript menu items, which refreshes the right-hand frame, doesn't raise // the LoadCompleted event. } }

提前谢谢。

Fernando Ronci



Thank you in advance.
Fernando Ronci

推荐答案

IE11边缘模式不支持vbscript。您将无法使用vbscript标记和脚本来重新启动路由器。 ( IE11中的兼容性更改) />


要强制使用WBC的WPF应用程序使用支持VBScript的仿真模式,您必须将FEATURE_CONTROL_EMULATION设置为IE10或更低。 (

Internet Explorer功能控制键



固件资源将在dll文件中,并使用res:protocol从frameset页面调用。



获取html资源的来源我会使用ResHacker(64位版本)或类似的资源查看器...(我想你也可以将dll加载到Visual Studio中查看它的PE结构。)


您可以尝试使用普通的桌面IE浏览器进行调试,然后使用Developer工具检查Source(DOM Explorer或Debug选项卡),但我不认为它会显示嵌入资源的来源(使用res:protocol)...
$


我调试自己的html res:资源放置一个故意错误地进入编译资源(例如var foo = 1/0;)(假设您有权访问dll资源) 并将Developer工具的Debug选项卡配置为&quo t;打破所有例外"。
这是我找到使用浏览器开发工具调试res:资源的唯一方法。



我不确定你应该问哪个论坛。 ..这个用于网站开发的html,css和脚本。



问候。

IE11 Edge mode does not support vbscript. You won't be able to use vbscript tags and scripting to reboot the router. (Compatibility Changes in IE11 )

To force your WPF application using the WBC to use an Emulation mode that supports VBScript you will have to set the FEATURE_CONTROL_EMULATION to IE10 or lower. ( Internet Explorer Feature control keys )

The firmware resources will be in a dll file and will be called from frameset page using the res: protocol.

to obtain the source of a html resource I would use ResHacker (64 bit version) or a similar resource viewer... (I think you can also load the dll into Visual Studio to view its PE structure).

You could try debugging it with your normal desktop IE browser and then using the Developer tool to inspect the Source (DOM Explorer or Debug tab), but I don't think it will show the source for embedded resources (using res: protocol)...

I debug my own html res: resources by placing an error intentionally into the compiled resource (eg. var foo=1/0;)(assuming that you have access to the dll resources) and configuring the Developer tool's Debug tab to "Break on all exceptions". That is the only way I have found to debug res: resources using the browser Developer tool.

I am not sure which forum you should ask in... this one's for html, css and scripting for website development.

Regards.


这篇关于如何获取框架的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆