0
点赞
收藏
分享

微信扫一扫

win10 uwp 模拟网页输入

有时候需要获得网页的 js 执行后的源代码,或者模拟网页输入,如点按钮输入文字。

如果需要实现,那么就需要用 WebView ,使用方法很简单。

首先创建一个 WebView ,接下来的所有输入都需要在 NavigationCompleted 之后才可以使用。

所以我就在构造方法使用下面代码

webView.Navigate(new Uri("https://www.bing.com/"));
    webView.NavigationCompleted += webView_NavigationCompletedAsync;

然后就可以在 加载完成的函数 获得加载完成网页的源代码。

private async void webView_NavigationCompletedAsync(WebView sender, WebViewNavigationCompletedEventArgs args)
{
    str = await webView.InvokeScriptAsync("eval", new string[] { "document.documentElement.outerHTML;" });
}

用到的方法就是 webView.InvokeScriptAsync 使用 js 代码。

如果需要在指定的文本框输入文字,可以使用下面代码

private async void EnterTextAsync(string text,string enterText)
{
    var functionString = string.Format(@"document.getElementsByClassName('{0}}')[0].innerText = '{1}';",text, enterText);
    await webView.InvokeScriptAsync("eval", new string[] { functionString });
}

看起来这些都是 js 的知识,难度不高。

点击按钮可以使用下面代码

private async void SimulateClickAsync(string button)
{
    var functionString = string.Format(@"ddocument.getElementsByClassName('{0}')[0].click();",button);
    await webView.InvokeScriptAsync("eval", new string[] { functionString });
}

更多的请去了解 js 的知识

参见:https://stackoverflow.com/questions/44685469/programatically-press-a-button-in-a-website-from-an-app-in-a-phone/44692971








举报

相关推荐

0 条评论