当前位置:网站首页>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
边栏推荐
- Can't install the Vmware virtual machine on the Ark Kai server?
- 如何在 PC 机上测试移动端的网页?
- International Soil Modeling Consortium-ISMC
- 太细了!阿里大佬耗时39天整理出一份Redis进阶笔记,满满的干货
- Jenkins使用pipeline部署服务到远程服务器
- 【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例...
- An in-depth understanding of the implementation principle of Hybrid
- What is hardware integrated development?What are the cores of hardware integrated development?
- What is test development and why is it so popular now?
- 本机号码一键登录原理
猜你喜欢
随机推荐
EPIC是什么平台?
总结篇4:redis 核心数据存储结构及核心业务模型实现应用场景
eyb:Redis学习(3)
浅谈如何保证Mysql主从一致
消防安全培训|暑期“消防课堂”,开讲!
API接口是什么?API接口常见的安全问题与安全措施有哪些?
史上最全架构师知识图谱
以技术御风险,护航云原生 | 同创永益 X 博云举办产品联合发布会
为了高性能、超大规模的模型训练,这个组合“出道”了
How tall is the B+ tree of the MySQL index?
我不写单元测试,被批了
自动生成设备节点
Substrate 源码更新导读八月第1周: 新版事务化存储层启用默认模式, Polkadot v0.9.27发布
An in-depth understanding of the implementation principle of Hybrid
Volatile:JVM 我警告你,我的人你别乱动
【.NET 6】开发minimal api以及依赖注入的实现和代码演示
Jenkins使用pipeline部署服务到远程服务器
JVM内存模型和结构详解(五大模型图解)
进程的两种创建方式,join方法,进程间的数据隔离,队列,进程间的通信IPC机制,生产者消费者模型,守护进程,僵尸进程,孤儿进程,互斥锁
MySQL的索引你了解吗



![[Pycharm easy to use function]](/img/f8/4c131516033286ba8bcb511d395462.png)





