当前位置:网站首页>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
边栏推荐
- 记一次 .NET 某工控自动化控制系统 卡死分析
- 微服务:事务管理
- 学长告诉我,大厂MySQL都是通过SSH连接的
- Smart Tool Management System
- Discuz! Forum program installation + template configuration tutorial
- mysql生成随机姓名、手机号、日期
- 谭中意:你知道 “开源女王” 是谁吗?
- A carnival of art and technology, cloud XR supports Anaya 2022 Sandbox Immersive Art Season
- 郭炜(郭大侠):九个关于开源的 Yes or No
- 消防安全培训|暑期“消防课堂”,开讲!
猜你喜欢
Self-taught software testing, how far can I go out to find a job?
Redis 定长队列的探索和实践
JMeter notes 6 | JMeter recording agent (configuration)
艺术与科技的狂欢,云端XR支撑阿那亚2022砂之盒沉浸艺术季
总结篇4:redis 核心数据存储结构及核心业务模型实现应用场景
消防安全培训|暑期“消防课堂”,开讲!
ABP 6.0.0-rc.1的新特性
【.NET 6】开发minimal api以及依赖注入的实现和代码演示
从事软件测试一年,只会基础的功能测试,怎么进一步学习?
【Pycharm好用功能】
随机推荐
WPF 实现柱形统计图
有什么好的开源自动化测试框架可以推荐?
leetcode/链表中环的入口节点
WinForm(三)揭开可视化控件的面纱
.NET 6学习笔记(4)——解决VS2022中Nullable警告
Guo Wei (Guo Daxia): Nine Yes or No about open source
Volatile:JVM 我警告你,我的人你别乱动
Fees and inquiry methods of futures account opening exchanges
以技术御风险,护航云原生 | 同创永益 X 博云举办产品联合发布会
Detailed explanation of JVM memory model and structure (five model diagrams)
Substrate 源码更新导读八月第1周: 新版事务化存储层启用默认模式, Polkadot v0.9.27发布
【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例...
JVM:(八)运行时数据区之方法区
【机器学习】回归树生成过程及举例理解
50道Redis面试题,来看看你会多少?
一文深入了解 Hybrid 的实现原理
Ark Standalone/Administrator Special Item Command Codes
Tan Zhongyi: Do you know who the "Queen of Open Source" is?
mysql generates random name, mobile number, date
ref的使用