How to get inner html source from WebView in Xamar

2020-02-14 20:09发布

I am using WebView to load my authentication web page. After the successful authentication, I need to get some values from html source content. can anyone helm to read the source html from WebView?.

var webView= new WebView {
  Source = "https://www.MyLoginSite.com"
};

I have checked this thread : Xamarin: How to get HTML from page in WebView?

I have written the below code after seeing above thread.

webView.Eval("document.body.innerHTML")

But the Eval is the void function, so how can I read value in C# code-behind or is there any easy alternative method to get entire html content as string?

2条回答
女痞
2楼-- · 2020-02-14 20:57

Here is my code:

 private async Task<string> GetHtml()
        {
            return await LoginWebViewItem.EvaluateJavaScriptAsync("document.body.innerHTML");
        }

Then I call this from my method:

string AllHtml = await GetHtml();
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-02-14 20:58

You will need to use HybridWebView from Xamarin-Forms-Labs and register a callback. Documentation is here.

In your C# code register a callback:

this.hybridWebView.RegisterCallback("dataCallback", t => System.Diagnostics.Debug.WriteLine(t));

Then evaluate some JS code calling the callback

this.hybridWebView.Eval("Native(\"dataCallback\", document.body.innerHTML);");
查看更多
登录 后发表回答