如何获取WebBrowser控件中的页面标题?
当我导航到不同的网站时,如何获取 WebBrowser 控件中的页面标题?
开头的xmlns属性
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
以 D
DataContext
DesiredSize
Dispatcher
DoubleTap
xaml 标记
<phone:WebBrowser Name="browser" Height="760" VerticalAlignment="Top"></phone:WebBrowser>
How can I get the page title in a WebBrowser control when I navigate to different websites?
xmlns
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
Properties starting with D
DataContext
DesiredSize
Dispatcher
DoubleTap
xaml tag
<phone:WebBrowser Name="browser" Height="760" VerticalAlignment="Top"></phone:WebBrowser>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我也有同样的问题。 @Akash Kava 的答案几乎是正确的,但这是读取 html 标题的正确 javascript:
I had the same problem. @Akash Kava's answer is almost correct but this is the correct javascript to read the html title:
对我来说,以下代码有效。 @Akash 和 @Mikko 的答案让我走上了正确的道路,但我在一些网站上仍然遇到了一些问题。据我了解,问题是当 WebBrowser 组件开始从远程服务器获取数据时,会引发 Navigated 事件。因此 DOM 对象尚未完成,因此调用
document.title
会引发错误。所以我只是在几毫秒后重试,直到获得标题。在我测试的任何网站上,这个“循环”从未迭代超过 3 次,并且每次都完美地给我带来了标题。For me the following code works. The answers from @Akash and @Mikko set me on the right path, but I still had some problems with a few websites. The problem as I understand it is that the Navigated event is raised when the
WebBrowser
component starts getting data from the remote server. As such the DOM object is not yet complete, so calling thedocument.title
throws an error. So I just retry after a few milliseconds till I get the title. This "loop" has never iterated more than 3 times on any website I tested and flawlessly brought me the title every time.我很确定这
应该可以解决问题。
请参阅此处。
I'm pretty sure that
should do the trick.
See here.
所有答案并非 100% 正确:
您必须调用以下内容:
String title = (string)browser.InvokeScript("eval", "document.title.toString()");
在浏览器的 LoadCompleted 事件中,而不是在 navigated 事件中。
All answers are not 100% correct:
You must call the following:
String title = (string)browser.InvokeScript("eval", "document.title.toString()");
in the LoadCompleted event of the browser, and not in the navigated event.
您可以使用 InvokeScript 获取标题因为,
我不知道它是否正确,但你也可以尝试 window.title 。
You can use InvokeScript to get title as,
I don't know it's correct or not but you can try window.title too.
下面的代码对我有用,请注意导航事件,如果您使用已加载,它将在页面加载之前触发,您希望它在页面完全加载“之后”的某个时间触发,导航充当该事件。
The code below works for me, note the navigated event, if you use loaded it will trigger just before the page is loaded, you want that to trigger sometime "after" the page is Fully Loaded, navigated acts as that event.