zoukankan      html  css  js  c++  java
  • 2018-2-13-win10-uwp-模拟网页输入

    title author date CreateTime categories
    win10 uwp 模拟网页输入
    lindexi
    2018-2-13 17:23:3 +0800
    2018-2-13 17:23:3 +0800
    Win10 UWP

    有时候需要获得网页的 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}';", "lindexi_gd@163.com");
                    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

  • 相关阅读:
    腾讯微博登录组件
    daffodil
    素数是个什么东西 prime number
    sama5d3 xplained 文件系统配置IP,系统复位后IP丢失[已解决]
    sama5d3 xplained 系统加载后确认使用的网口
    SAMA5D3 Xplained 开发板烧写官方固件后启动失败【已解决】
    linux文件系统解压 tar: Exiting with failure status due to previous errors [Solved]
    安装POSIX man 手册
    老子《道德经》第四十三章
    老子《道德经》第四十二章
  • 原文地址:https://www.cnblogs.com/lindexi/p/12086502.html
Copyright © 2011-2022 走看看