当前位置:网站首页>win10 uwp 模拟网页输入
win10 uwp 模拟网页输入
2022-08-09 16:53:00 【林德熙】
有时候需要获得网页的 js 执行后的源代码,或者模拟网页输入,如点按钮输入文字。
如果需要实现,那么就需要用 WebView ,使用方法很简单。
首先创建一个 WebView ,接下来的所有输入都需要在 NavigationCompleted 之后才可以使用。
所以我就在构造方法使用下面代码
webView.Navigate(new Uri("https://www.bing.com/"));
webView.NavigationCompleted += webView_NavigationCompletedAsync;
在模拟输入之前,如果需要在 UWP 使用 Webview 获取网页源代码,那么需要在 加载完成的函数 使用下面的代码来 获得加载完成网页的源代码。
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(@"document.getElementsByClassName('{0}')[0].click();",button);
await webView.InvokeScriptAsync("eval", new string[] { functionString });
}
如果需要填写表单 form 那么前面使用的innerText
需要修改为value,建议打开 edge 在控制命令输入,尝试一个正确的输入
更多的请去了解 js 的知识
UWP webView 模拟登陆 csdn
下面给大家一个叫简单方法模拟登陆csdn
GeekWebView.Navigate(new Uri("http://passport.csdn.net/"));
GeekWebView.NavigationCompleted += OnNavigationCompleted;
F = async () =>
{
var functionString = string.Format(@"document.getElementsByName('username')[0].value='{0}';", "[email protected]");
await GeekWebView.InvokeScriptAsync("eval", new string[] { functionString });
functionString = string.Format(@"document.getElementsByName('password')[0].value='{0}';", "密码");
await GeekWebView.InvokeScriptAsync("eval", new string[] { functionString });
functionString = string.Format(@"document.getElementsByClassName('logging')[0].click();");
await GeekWebView.InvokeScriptAsync("eval", new string[] { functionString });
};
private Action F { set; get; }
private void OnNavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
F();
}
使用 cookie
如果需要使用 cookie 那么请加上下面的代码
Windows.Web.Http.Filters.HttpBaseProtocolFilter filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
只要写上这句话就好了
参见:https://stackoverflow.com/questions/44685469/programatically-press-a-button-in-a-website-from-an-app-in-a-phone/44692971
边栏推荐
猜你喜欢
Discuz! Forum program installation + template configuration tutorial
谭中意:你知道 “开源女王” 是谁吗?
太细了!阿里大佬耗时39天整理出一份Redis进阶笔记,满满的干货
试试使用 Vitest 进行组件测试,确实很香。
crm系统哪家好?好用的crm管理系统推荐
An in-depth understanding of the implementation principle of Hybrid
Functions and Features of Smart Home Control System
2022秋招面试宝典,啃完面试稳了
《.NET物联网从零开始》系列
学长告诉我,大厂MySQL都是通过SSH连接的
随机推荐
mysql生成随机姓名、手机号、日期
Jenkins使用pipeline部署服务到远程服务器
[Pycharm easy to use function]
【燃】是时候展现真正的实力了!一文看懂2022华为开发者大赛技术亮点
leetcode/链表中环的入口节点
mysql generates random name, mobile number, date
国际土壤模型协会 International Soil Modeling Consortium-ISMC
WPF效果第一百九十四篇之伸缩面板
International Soil Modeling Consortium-ISMC
BSN季度版本2022年8月31日迭代更新预告
怎样选择一个好的SaaS知识库工具?
手写flexible.js的原理实现,我终于明白移动端多端适配
.NET Community Toolkit 8.0.0 版本发布
Jenkins deploys services to remote servers using pipelines
【解决】虚拟机VMware通过局域网连接机器人no route to host
What you should know about futures account opening
一口气拿下6个大厂offer,是什么水平
郭炜(郭大侠):九个关于开源的 Yes or No
[SUCTF 2019]CheckIn
【ROS2原理9】 QoS - 截止日期、活跃度和寿命