我打算从网站获取dom元素的内容,我无法使用
var html = await client.GetAsync("http://example.com/test.html");
作为JS执行后填充特定的dom元素 我可以使用 webview 来托管内容,但这并不能解决问题 我无法使用HtmlDocument.GetElementById Method (String)要么因为它位于 System.Windows.Forms 命名空间下,但通用 Windows 平台不支持该命名空间! 另一种选择是 InvokeAsync
await webView5.InvokeScriptAsync("doSomething", null);
如 XAML WebView control sample 的场景 5 所示 但这只是触发事件,并不能帮助获取 dom 元素(甚至在 JS 执行完成后获取源代码)。 //该应用程序是 C# UWP,而不是 Winjs
最佳答案
您可以使用HtmlAgilityPack库来查询您下载的 DOM。请参阅example here或者搜索更多,有很多。
或者,您可以运行 javascript,它会执行您需要的操作并返回包含您的 ID 的字符串。如您所见,InvokeScriptAsync
返回 String
,其中可以包含您的 javascript 返回的任何内容。例如,this is how使用 javascript 获取 DOM:
var result = await this.webView.InvokeScriptAsync("eval", new[] { "document.documentElement.outerHTML;" });
关于javascript - 在WebView UWP中通过ID获取dom元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45807976/