当前位置:网站首页>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
边栏推荐
猜你喜欢
The principle implementation of handwritten flexible.js, I finally understand the multi-terminal adaptation of the mobile terminal
The senior told me that the MySQL of the big factory is connected through SSH
WinForm(三)揭开可视化控件的面纱
在 .NET MAUI 中如何更好地自定义控件
艺术与科技的狂欢,云端XR支撑阿那亚2022砂之盒沉浸艺术季
[Pycharm easy to use function]
uniapp电影购票选座系统源码
Metasploit——辅助模块(Auxiliary)
What platform is EPIC?
浅谈如何保证Mysql主从一致
随机推荐
Jenkins使用pipeline部署服务到远程服务器
JMeter notes 6 | JMeter recording agent (configuration)
Substrate 源码更新导读八月第1周: 新版事务化存储层启用默认模式, Polkadot v0.9.27发布
Experience far more than Hue, this is the favorite SQL tool for technicians
One-key login principle of local number
艺术与科技的狂欢,云端XR支撑阿那亚2022砂之盒沉浸艺术季
Discuz!论坛程序安装+模板配置教程
What is hardware integrated development?What are the cores of hardware integrated development?
The difference between approach and method
程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
【ROS2原理9】 QoS - 截止日期、活跃度和寿命
本机号码一键登录原理
怎样选择一个好的SaaS知识库工具?
进行知识管理的好处有哪些?
【工业数字化大讲堂 第二十一期】企业数字化能碳AI管控平台,特邀技术中心总经理 王勇老师分享,8月11日(周四)下午4点
【机器学习】回归树生成过程及举例理解
Detailed explanation of JVM memory model and structure (five model diagrams)
【工业数字化大讲堂 第二十期】制造业数字化能力建设分享,特邀制造业高级咨询顾问 李东老师分享
如何在 PC 机上测试移动端的网页?
Axure实现表格带滚动条