我试图创建网页自动登录并在通过控制台应用程序登录时显示页面。我还创建了一个xml,以便他们用户可以将登录信息添加到xml中,而不是vai代码中。该应用程序将在xml页面上查找登录详细信息,然后登录到该网页。我使用WatIn来做到这一点,但是却得到了[无法从'Watin.Core.Constraints.Attribute Constraint'转换为'System.Text.RegularExpressions.Regex']的人可以协助我解决这个问题,或指导我采取更好的方法这个。
我的C#代码:
class Program
{
private static string url;
private static string username;
private static string password;
private static string webUserID;
private static string webPassID;
private static string webLoginID;
[STAThread]
static void Main(string[] args)
{
string path = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "TheXMLFile.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
foreach (XmlNode n in xmlDoc.DocumentElement.ChildNodes)
{
if (n.Attributes["key"].Value == "url")
{
url = n.Attributes["value"].Value;
}
if (n.Attributes["key"].Value == "username")
{
username = n.Attributes["value"].Value;
}
if (n.Attributes["key"].Value == "password")
{
password = n.Attributes["value"].Value;
}
if (n.Attributes["key"].Value == "webUsernameID")
{
webUserID = n.Attributes["value"].Value;
}
if (n.Attributes["key"].Value == "webPasswordID")
{
webPassID = n.Attributes["value"].Value;
}
if (n.Attributes["key"].Value == "webLoginButtonID")
{
webLoginID = n.Attributes["value"].Value;
}
}
RunOnStaThread<object>(() =>
{
IE ie = new IE(url);
ie.FindText(Find.ById(webUserID)).TypeText(username); //This is where the error is.
ie.FindText(Find.ById(webPassID)).TypeText(password); //This is where the error is.
ie.Button(Find.ByValue(webLoginID)).Click();
return null;
});
}
我的XML:
<?xml version="1.0" encoding="utf-8" ?>
<Website>
<Url key="url" value="webpage url" />
<Username key="username" value="Login details" />
<Password key ="password" value="Login password" />
<WebUsernameID key="webUsernameID" value="username" />
<WebPasswordID key="webPasswordID" value="password" />
<WebLoginButtonID key="webLoginButtonID" value="submit" />
</Website>