简体   繁体   中英

C++/WinRT WinUI3: How to get html source code by WebView2?

I'm trying to use a webview2 control to get the body text of a website. But nothing showed.

My code:

winrt::Windows::Foundation::IAsyncAction MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&)
    {
        myButton().Content(box_value(L"Clicked"));
        winrt::Microsoft::UI::Xaml::Controls::WebView2 web;
        std::wstring uriToLaunch{ L"http://www.bing.com" };
        Windows::Foundation::Uri uri{ uriToLaunch };
        web.Source(uri);
        co_await web.EnsureCoreWebView2Async();
        winrt::hstring obj = co_await web.CoreWebView2().ExecuteScriptAsync(L"document.body.innerText");
        TextBlock().Text(obj);
        web.close();
    }

Thank you indeed for your advise in advance.

You might need to change your script. It should be like ExecuteScriptAsync("document.documentElement.outerHTML;")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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