我正在尝试创建一个自动将我登录到网页的应用。 页面不是我的,所以我看不到它背后的代码。 http://pcc.magister.net(第一个通知屏幕后的页面)
现在我设法插入了用户名和密码,但我似乎无法触发点击页面上的按钮。我得到了这个:
var functionString = string.Format(@"document.getElementsByClassName('primary-btn ripple')[0].click();");
await Display.InvokeScriptAsync("eval", new string[] { functionString });
但我得到的只是错误。 ' System.Exception的'在System.Private.CoreLib.ni.dll和0x80020101事件中,这是一般的javascript错误。
DOM资源管理器中的按钮如下所示:
<mg-button is-loading="$ctrl.isLoading" caption="doorgaan">
<button class="primary-button ripple" type="submit" ng-class="{'active': $ctrl.isLoading}">
</button></mg-button>
任何比我更聪明的人看到我在这里做错了什么?
这是一个最小的c#:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Diagnostics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace Test
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
Display.Navigate(new Uri("http://pcc.magister.net/"));
}
private async void NewPageLoaded(WebView sender, WebViewNavigationCompletedEventArgs args)
{
string functionString = "";
if (Display.Source.AbsolutePath.ToString() == "/account/login")
{
functionString = String.Format("document.getElementById('username').innerText = 'testuser'");
try
{
await Display.InvokeScriptAsync("eval", new string[] { functionString });
}
catch
{
//
}
functionString = string.Format("document.getElementsByClassName('primary-btn ripple')[0].click();");
try
{
await Display.InvokeScriptAsync("eval", new string[] { functionString });
}
catch (Exception ezz)
{
Debug.WriteLine("There was an exception!" + ezz);
}
}
}
}
}
最小的XAML:
<Page
x:Class="Test.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Test"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="0,0,0,0" Name="mainGrid">
<WebView Margin="0,0,0,0" Name="Display" NavigationCompleted="NewPageLoaded"/>
</Grid>
</Page>