我所需要的是使用全部循环登录多个帐户(不是一次全部)的更可靠和稳定的方式。我已经有一个有效的示例代码,但并不是那么稳定。
string email_1 = "email_1";
string password_1 = "password_1";
string email_2 = "email_2";
string password_2 = "password_2";
int k = 1;
do
{
string e = null;
string p = null;
if (k == 1)
{
e = email_2;
p = password_2;
}
if (k == 2)
{
e = email_1;
p = password_1;
}
_driver.FindElement.(id("submit_email")).sendkeys(e);
IWebElement s = _driver.FindElement.(id("submit_password"));
s.Sendkeys(p);
s.Submit();
//do stuff
_driver.FindElement(id("logout")).Click();
k++;
} while (k <= 2);
有关如何使其更稳定可靠,或者更好的方法的任何想法?
答案 0 :(得分:4)
您需要完全重构此代码。你有没有练过面向对象的设计?
创建此类:
public class Account // Stores the email and password of each account
{
public string Email;
public string Password;
public Account(string email, string password) // Constructor
{
Email = email;
Password = password;
}
}
然后像这样使用这个类:
// Keep all the accounts in one place
List<Account> accounts = new List<Account>()
{
new Account("email_1", "password_1"), // Create a new account
new Account("email_2", "password_2") // Create another account
};
foreach(Account account in accounts)
{
_driver.FindElement.(id("submit_email)).sendkeys(account.Email);
IWebElement s = _driver.FindElement.(id("submit_password"));
s.Sendkeys(account.Password);
s.Submit();
// do stuff
_driver.FindElement(id("logout")).Click();
}
此代码具有高度可扩展性和可重用性。如果您要添加其他帐户,请使用new Account(string, string),
中的List
语句,这是唯一需要进行的更改。