簡體   English   中英

如何在Android Webview中輸入文本並單擊按鈕? (C#Xamarin)

[英]How to enter text and click buttons in android Webview? (c# Xamarin)

我在樹莓派上運行了mjpg流媒體,並且我制作了一個Android應用程序來觀看流,我只是在其中放了一個webview並使其導航到我的流媒體URL,但是現在我在mjpg流媒體上啟用了密碼功能。 當我進入頁面時,會彈出一個對話框,要求輸入用戶名和密碼:

登錄提示

現在,我需要我的應用程序每次以某種方式自動輸入用戶名和密碼。

我已經進行了一些研究,我唯一能找到的是用於C#Windows窗體的Web瀏覽器控件:

webBrowser1.Document.GetElementById("username").SetAttribute("value", "supersecretusername");
webBrowser1.Document.GetElementById("Log_in").InvokeMember("click");

但不要認為這些功能適用於android的webview和javascript的警報。

有沒有辦法在這些字段中輸入用戶名和密碼並自動單擊按鈕,還是有辦法一次輸入並以某種方式保存它們?

我還應該提到,我是編程新手,而且我不是英語母語者(您可能會說)。謝謝。

您可以使用LoadUrl方法將JavaScript注入WebView。 這里是一個簡單的示例,它如何在Android中工作:

1)獲得WebView引用后,您需要將JavaScript設置為啟用

        var webBrowser = this.FindViewById<WebView>(Resource.Id.webView1);
        webBrowser.Settings.JavaScriptEnabled = true;

2)要處理加載事件,您需要分配一個WebViewClient

        webBrowser.SetWebViewClient(new Client());

3)出於演示目的,我創建了一個示例HTML頁面並從資產中加載它:

        webBrowser.LoadUrl("file:///android_asset/main.html");

4)客戶本身

public class Client : WebViewClient
{
    public override void OnPageFinished(WebView view, string url)
    {
        base.OnPageFinished(view, url);

        if (!url.Contains("main.html")) return;

        var username = "supersecretusername";
        var password = "supersecretpassword";

        // to ensure Log_in click isn't called before username & password are set lets
        // create the script as single item instead of calling them separately
        var script = $"document.getElementById(\"username\").value = \"{username}\";" +
                     $"document.getElementById(\"password\").value = \"{password}\";" +
                     "document.getElementById(\"Log_in\").click()";

        view.LoadUrl($"javascript: {script}");

        // separate calls
        //view.LoadUrl("javascript: document.getElementById(\"username\").value = \"sami\";document.getElementById(\"password\").value = \"password\";");
        //view.LoadUrl("javascript: document.getElementById(\"password\").value = \"password\"");
        //view.LoadUrl("javascript: document.getElementById(\"Log_in\").click()");
    }
}

這是對我有用的(VS2017):

MainPage.xaml中

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MyApp"
             x:Class="MyApp.MainPage"
             NavigationPage.HasNavigationBar="False"
             Title="My App">
    <WebView x:Name="browser" Navigating="OnNavigating" Navigated="OnNavigated"></WebView>
</ContentPage>

MainPage.xaml.cs中

using System;
using Xamarin.Forms;

namespace MyApp
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            browser.Source = "..."; // or in xaml
        }

        // Event when webview has finished change
        void OnNavigated(object sender, WebNavigatedEventArgs args)
        {
            string script = "...;";
            browser.EvaluateJavaScriptAsync(script);
        }

        // ...
    }
}

https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.webview.evaluatejavascriptasync?view=xamarin-forms#Xamarin_Forms_WebView_EvaluateJavaScriptAsync_System_String_

您張貼在錯誤的部分。 您的問題也不是很清楚。 這是您的本機程序嗎? 您對源有完全訪問權嗎?

據我了解,您正在尋找的是移動自動化。

查找機器人框架appium 用於移動自動化和測試。 Appium是硒的移動等同物,而機器人框架只是python包裝器,因此,如果您還不了解python,則實際上並沒有強迫他學習它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM