如何使用selenium c#webdriver输入选定的密码字符

本文关键字:密码 字符 输入 何使用 selenium c#webdriver | 更新日期: 2023-09-27 18:01:12

我需要为网站的登录功能编写c#Selenium代码。

提示用户从密码中输入三个不同的字符,例如第3、第6和第8个字符,每个字符都在单独的文本字段中;每次用户尝试登录时,请求的字符都会发生变化,因此我很难实现自动化。在我的测试环境中,密码总是设置为TestPassword

HTML为与请求的字符相关的文本字段分配一个Id,例如下面是对第5个字符的请求。这对其他每个字符都是一样的,但是,数字会更改以表示请求的密码的字符。

<div class="form-type-password form-item-password-challenge-challenges-5 form-item form-group">
<label for="edit-password-challenge-challenges-5">
5
<sup>th</sup>
</label>
<input id="edit-password-challenge-challenges-5" class="form-control form-text" type="password" maxlength="1" size="1" name="password_challenge[challenges][5]" autocomplete="off" tabindex="2" placeholder="-"/

可以要求输入字符1到10。在C#中使用Selenium是否可以实现自动化?

非常感谢

如何使用selenium c#webdriver输入选定的密码字符

以下内容应该适用于您。

 //Your password string
const string testPassword = "TestPassword";
//find the id attribute that contains the index of character needs to be input
string str= driver.FindElement(By.CssSelector("input[id^='edit-password-challenge']")).GetAttribute("id");
//find the index of the char
int index = Convert.ToInt16(Regex.Match(str, "[0-9]+").ToString().Replace("'"", ""));
//find the char and convert to string so that can be used with SendKeys() method
string charToInput = testPassword[index-1].ToString();
//input the password using the SendKeys() method
driver.FindElement(By.CssSelector("the selector")).SendKeys(charToInput);