简体   繁体   English

Webbrowser控件未显示HTML,但显示了网页

[英]Webbrowser control is not showing Html but shows webpage

I am automating a task using webbrowser control , the site display pages using frames. 我正在使用webbrowser控件自动化任务,该站点使用框架显示页面。 My issue is i get to a point , where i can see the webpage loaded properly on the webbrowser control ,but when it gets into the code and i see the html i see nothing. 我的问题是,我可以看到在webbrowser控件上正确加载了网页,但是当它进入代码并且看到html时,我什么也看不到。

I have seen other examples here too , but all of those do no return all the browser html. 我在这里也看到了其他示例,但是所有这些示例都不会返回所有浏览器html。

What i get by using this: 我通过使用这个得到什么:

                    HtmlWindow frame = webBrowser1.Document.Window.Frames[1];
                    string str = frame.Document.Body.OuterHtml;

Is just : 只是 :

The main frame tag with attributes like SRC tag etc, is there any way how to handle this?Because as i can see the webpage completely loaded why do i not see the html?AS when i do that on the internet explorer i do see the pages source once loaded why not here? 具有诸如SRC标签等属性的主框架标签,有什么方法可以处理此问题?因为我可以看到网页已完全加载,为什么我在Internet Explorer上看到HTML时却看不到html?AS页面源一旦加载,为什么不在这里?

ADDITIONAL INFO 附加信息

There are two frames on the page : 页面上有两个框架:

i use this to as above: 我如上所述

HtmlWindow frame = webBrowser1.Document.Window.Frames[0]; HtmlWindow frame = webBrowser1.Document.Window.Frames [0];

        string str = frame.Document.Body.OuterHtml;

And i get the correct HTMl for the first frame but for the second one i only see: 我为第一帧获得了正确的HTMl,但是对于第二帧,我只看到了:

<FRAMESET frameSpacing=1 border=1 borderColor=#ffffff frameBorder=0 rows=29,*><FRAME title="Edit Search" marginHeight=0 src="http://web2.westlaw.com/result/dctopnavigation.aspx?rs=WLW12.01&amp;ss=CXT&amp;cnt=DOC&amp;fcl=True&amp;cfid=1&amp;method=TNC&amp;service=Search&amp;fn=_top&amp;sskey=CLID_SSSA49266105122&amp;db=AK-CS&amp;fmqv=s&amp;srch=TRUE&amp;origin=Search&amp;vr=2.0&amp;cxt=RL&amp;rlt=CLID_QRYRLT803076105122&amp;query=%22LAND+USE%22&amp;mt=Westlaw&amp;rlti=1&amp;n=1&amp;rp=%2fsearch%2fdefault.wl&amp;rltdb=CLID_DB72585895122&amp;eq=search&amp;scxt=WL&amp;sv=Split" frameBorder=0 name=TopNav marginWidth=0 scrolling=no><FRAME title="Main Document" marginHeight=0 src="http://web2.westlaw.com/result/dccontent.aspx?rs=WLW12.01&amp;ss=CXT&amp;cnt=DOC&amp;fcl=True&amp;cfid=1&amp;method=TNC&amp;service=Search&amp;fn=_top&amp;sskey=CLID_SSSA49266105122&amp;db=AK-CS&amp;fmqv=s&amp;srch=TRUE&amp;origin=Search&amp;vr=2.0&amp;cxt=RL&amp;rlt=CLID_QRYRLT803076105122&amp;query=%22LAND+USE%22&amp;mt=Westlaw&amp;rlti=1&amp;n=1&amp;rp=%2fsearch%2fdefault.wl&amp;rltdb=CLID_DB72585895122&amp;eq=search&amp;scxt=WL&amp;sv=Split" frameBorder=0 borderColor=#ffffff name=content marginWidth=0><NOFRAMES></NOFRAMES></FRAMESET>

UPDATE 更新

The two url of the frames are as follows : 框架的两个网址如下:

Frame1 whose html i see 我看到其HTML的Frame1

http://web2.westlaw.com/nav/NavBar.aspx?RS=WLW12.01&VR=2.0&SV=Split&FN=_top&MT=Westlaw&MST=

Frame2 whose html i do not see: Frame2的html我看不到:

http://web2.westlaw.com/result/result.aspx?RP=/Search/default.wl&action=Search&CFID=1&DB=AK%2DCS&EQ=search&fmqv=s&Method=TNC&origin=Search&Query=%22LAND+USE%22&RLT=CLID%5FQRYRLT302424536122&RLTDB=CLID%5FDB6558157526122&Service=Search&SRCH=TRUE&SSKey=CLID%5FSSSA648523536122&RS=WLW12.01&VR=2.0&SV=Split&FN=_top&MT=Westlaw&MST=

And the properties of the second frame whose html i do not get are in the picture below: 下图显示的是我无法获取html的第二帧的属性:

在此处输入图片说明

Thank you 谢谢

I paid for the solution of the question above and it works 100 %. 我为上述问题的解决方案付费,并且可以100%使用。

What i did was use this function below and it returned me the count to the tag i was seeking which i could not find :S.. Use this to call the function listed below: 我所做的是使用下面的此函数,它使我将计数返回到要查找的标签,而我找不到:S。使用此函数可以调用下面列出的函数:

FillFrame(webBrowser1.Document.Window.Frames);



private void FillFrame(HtmlWindowCollection hwc)
        {


            if (hwc == null) return;
            foreach (HtmlWindow hw in hwc)
            {
                HtmlElement getSpanid = hw.Document.GetElementById("mDisplayCiteList_ctl00_mResultCountLabel");
                if (getSpanid != null)
                {

                    doccount = getSpanid.InnerText.Replace("Documents", "").Replace("Document", "").Trim();

                    break;
                }

                if (hw.Frames.Count > 0) FillFrame(hw.Frames);
            }


        }

Hope it helps people . 希望它能帮助人们。

Thank you 谢谢

For taking html you have to do it that way: 对于采用html,您必须这样做:

        WebClient client = new WebClient();
        string html = client.DownloadString(@"http://stackoverflow.com");

That's an example of course, you can change the address. 当然,这是一个示例,您可以更改地址。 By the way, you need using System.Net; 顺便说一句,您需要使用System.Net。

This works just fine...gets BODY element with all inner elements: 这很好用...使BODY元素具有所有内部元素:

Somewhere in your Form code: 表单代码中的某处:

wb.Url = new Uri("http://stackoverflow.com");
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wbDocumentCompleted);

And here is wbDocumentCompleted: 这是wbDocumentCompleted:

void wb1DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    var yourBodyHtml = wb.Document.Body.OuterHtml;
}

wb is System.Windows.Forms.WebBrowser wb是System.Windows.Forms.WebBrowser

UPDATE: 更新:

The same as for the document, I think that your second frame is not loaded at the time you check for it's content...You can try solutions from this link. 在相同的文档,我认为你的第二个框架是不是你检查它的内容时加载的......你可以尝试从解决这个链接。 You will have to wait for your frames to be loaded in order to see its content. 您必须等待框架加载才能看到其内容。

The most likely reason is that frame index 0 has the same domain name as the main/parent page, while the frame index 1 has a different domain name. 最可能的原因是帧索引0与主/父页面具有相同的域名,而帧索引1具有不同的域名。 Am I correct? 我对么?

This creates a cross-frame security issue, and the WB control just leaves you high and dry and doesn't tell you what on earth went wrong, and just leaves your objects, properties and data empty (will say "No Variables" in the watch window when you try to expand the object). 这会造成跨框架的安全问题,而WB控件只会让您高高在干,而不会告诉您到底出了什么问题,而只是让您的对象,属性和数据为空(将在窗口中显示“无变量”尝试扩展对象时,请观看窗口)。

The only thing you can access in this situation is pretty much the URL and iFrame properties, but nothing inside the iFrame. 在这种情况下,您唯一可以访问的就是URL和iFrame属性,但iFrame内部没有任何内容。

Of course, there are ways to overcome teh cross-frame security issues - but they are not built into the WebBrowser control, and they are external solutions, depending on which WB control you are using (as in, .NET version or pre .NET version). 当然,有一些方法可以解决跨框架安全问题-但它们并未内置在WebBrowser控件中,它们是外部解决方案,具体取决于您使用的是哪个WB控件(如.NET版本或.NET之前的版本)版)。

Let me know if I have correctly identified your problem, and if so, if you would like me to tell you about the solution tailored to your setup & instance of the WB control. 让我知道我是否正确识别了您的问题,如果可以,请让我告诉您有关为WB控件的设置和实例量身定制的解决方案的信息。

UPDATE : I have noticed that you're doing a .getElementByTagName("HTML")(0).outerHTML to get the HTML, all you need to do is call this on the document object, or the .body object and that should do it. 更新 :我注意到您正在执行.getElementByTagName(“ HTML”)(0).outerHTML以获取HTML,您所需要做的就是在文档对象或.body对象上调用此方法,并且应该这样做它。 MyDoc.Body.innerHTML should get the the content you want. MyDoc.Body.innerHTML应该获得所需的内容。 Also, notice that there are additional iFrames inside these documents, in case that is of relevance. 另外,请注意,如果相关,这些文档中还有其他iFrame。 Can you give us the main document URL that has these two URL's in it so we / I can replicate what you're doing here? 您能否给我们提供包含这两个URL的主文档URL,以便我们/我可以在这里复制您的操作? Also, not sure why you are using DomElement but you should just cast it to the native object it wants to be cast to, either a IHTMLDocument2 or the object you see in the watch window, which I think is IHTMLFrameElement (if i recall correctly, but you will know what i mean once you see it). 另外,不确定为什么要使用DomElement,但应该将其转换为要转换为的本机对象,即IHTMLDocument2或在监视窗口中看到的对象,我认为这是IHTMLFrameElement(如果我没记错的话,但是一旦您看到它,您就会知道我的意思。 If you are trying to use an XML object, this could be the reason why you aren't able to get the HTML content, change the object declaration and casting if there is one, and give it a go & let us know :). 如果您尝试使用XML对象,这可能就是为什么您无法获取HTML内容,更改对象声明并进行强制转换(如果有的话),然后继续执行并让我们知道的原因:)。 Now I'm curious too :). 现在我也很好奇:)。

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

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