gpt4 book ai didi

c# - 如何正确显示 WebBrowser 控件中加载的当前和真实 url?

转载 作者:行者123 更新时间:2023-11-30 21:27:39 26 4
gpt4 key购买 nike

在我的表单中,我添加了一个 WebBrowser 和一个 TextBox,我想在上面显示当前加载的 Url。

请注意 Microsoft Docs 所说的内容:

  • WebBrowser.Navigating事件:

    Occurs before the WebBrowser control navigates to a new document

  • WebBrowser.Navigated事件:

    Occurs when the WebBrowser control has navigated to a new document and has begun loading it.

    ...

    Handle the DocumentCompleted event to receive notification when the WebBrowser control finishes loading the new document.

  • WebBrowser.DocumentCompleted事件:

    Occurs when the WebBrowser control finishes loading a document

    ...

    Handle the DocumentCompleted event to receive notification when the new document finishes loading. When the DocumentCompleted event occurs, the new document is fully loaded

触发事件的顺序是:NavigatingNavigatedDocumentCompleted,所以我正在处理这些事件以尝试正确更新当前网址:

private void WebBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) {
this.TextBox1.Text = e.Url.ToString();
}

private void WebBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e) {
this.TextBox1.Text = e.Url.ToString();
}

private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
this.TextBox1.Text = e.Url.ToString();
}

问题是由于某些原因,某些网站的 url 似乎没有正确更新...

例如,当我在 Google 的搜索引擎中导航时,如果我确实点击了 Google 图片按钮,则 url 会更新为“http://www.google.com/blank.html”。此外,我在 TextBox 中显示的网址与我在 Firefox 或 Chrome 的地址栏中看到的网址完全不同;出于某种原因,我获得的网址在查询中有额外的参数。

自己看:

https://i.imgur.com/PQlSu47.gif

enter image description here

是否有任何解决方法可以改善这种烦人的行为,以便我可以像 Firefox 或 Chrome 一样高效地显示当前 url?。我的意思是,例如 Firefox 和 Chrome 不会在地址栏中显示“http://www.google.com/blank.html”,也不会显示带有附加参数的 url 查询,因为我必须显示(您可以在上面的 GIF 图像中看到)。

请注意,Google 网站的问题仅作为示例。我要求一个通用的解决方案,因为这个问题出现在更多的网站上。


另请注意,如果我使用的是 WebBrowser 组件 CefSharp的基于 chromium 的网络浏览器,调整我的代码以重现与我显示/更新当前 url 时相同的代码,然后问题部分消失了......

使用 CefSharp 在通过 Google Images 导航时不显示“http://www.google.com/blank.html”,但是 url 的查询仍然包含其他参数/​​与 Firefox 或 Chrome 浏览器中显示的 url 相比有很多差异。除此之外,我想避免使用 CefSharp 来解决这类问题......

最佳答案

长话短说

不要在事件参数中使用 URL,而是使用 Web 浏览器控件中的 URL:

string url = webBrowser1.Url.ToString();

长答案

您在这里缺少的是 HTML 页面可以包含 iframe元素。一个 iframe 封装了 HTML Window和包含的 HTML Document , 它执行自己的导航。 WebBrowser 控件的导航事件会为顶部窗口(您要为其显示 URL)和 iframe 触发。你必须区分这两者。

具体来说,在您的情况下 http://www.google.com/blank.html 来自 iframe:

<html> <!-- top window -->
<body> <!-- top window's document -->
<iframe src="http://www.google.com/blank.html">
<!--
here the browser will load and "insert" HTML of blank.html
lines below don't exist in the original HTML
they are loaded and "inserted" here by the browser
-->
<html> <!-- iframe's window -->
<body> <!-- iframe's window's document -->
<!-- the body can contain additional iframes... -->
</body>
</html>
</iframe>
</body>
</html>

一般来说,HTML 页面的 DOM 是一棵 HTML 窗口对象树,根窗口由 window.top 返回。属性(property)。根据页面的设计方式,iframe 可以显示或隐藏;它们可以由服务器以 HTML 呈现,也可以通过 JavaScript 在浏览器中动态地操作、创建或删除:

  • 创建新的 iframe 时,它会导航到其 src 属性中指定的 URL。如果既未指定src 也未指定嵌入内容,则导航 URL 将为 about:blank
  • 当修改现有 iframesrc 属性时,iframe 将导航到新的 src .
  • window.location修改了顶部或 iframe HTML 窗口,它将导航到新的 location

但是,确定哪个 HTML 窗口(顶部或 iframe)执行导航似乎不是一项简单的任务,因此更简单的方法是获取顶部窗口的 URL:

string url = webBrowser1.Url.ToString();

或在 DocumentCompleted 事件之后:

HtmlWindow topWindow = webBrowser1.Document.Window;
string url = topWindow.Url.ToString();

关于c# - 如何正确显示 WebBrowser 控件中加载的当前和真实 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57455881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com