我正在尝试编写一个脚本来登录需要通过令人难忘的单词进行身份验证的应用程序,但是该单词的长度为 10 位数字,并且按照以下方式随机选择 3 个字符:
Please enter characters 1, 2 and 8 from your memorable information then click on the continue button.
Character 1 :
Character 2 :
Character 8 :
下面是标签的代码:
<label for="frmentermemorableinformation1:strEnterMemorableInformation_memInfo1">Character 1 </label>
使用selenium WebDriver JAVA,如何实现?
因此,需要 IF 标签 = 1,然后从下拉列表中选择 A。我知道密码,只是不知道页面加载时会询问什么字符,因为这些字符每次都会改变,所以需要添加逻辑。
非常感谢。
最佳答案
也许您无法控制此页面,但通常网站使用密码而不是这种......奇怪的方式来验证安全性。如果您可以输入整个密码,则无需识别整个“易记单词”中的单个字符。无论如何...
我会做类似下面的事情。您知道令人难忘的单词,并且可以从“字符 1”、“字符 2”和“字符 8”等标签中找到索引。在本例中为 1、2、8。现在您将获得字符 1、2 、 和 8,然后将它们放入相关的下拉列表中。
public class Sample
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
String memorableWord = "memorable1";
// set up the locators for the labels that contain the information on which characters will be needed from the memorable word
By char1 = By.cssSelector("label[for='frmentermemorableinformation1:strEnterMemorableInformation_memInfo1']");
By char2 = By.cssSelector("label[for='frmentermemorableinformation2:strEnterMemorableInformation_memInfo2']");
By char3 = By.cssSelector("label[for='frmentermemorableinformation3:strEnterMemorableInformation_memInfo3']");
// get the indices from the labels
int index1 = getNumber(driver.findElement(char1).getText().trim());
int index2 = getNumber(driver.findElement(char2).getText().trim());
int index3 = getNumber(driver.findElement(char3).getText().trim());
// from your description, it sounds like the letters are being chosen from a SELECT so I used that in the code below
// I don't know how to find the SELECT since that HTML was not provided so I've used sample locators below
// basically it grabs the SELECT and then chooses the letter based on it's position in the memorable word
new Select(driver.findElement(By.id("test1"))).selectByVisibleText(memorableWord.substring(index1, index1 + 1));
new Select(driver.findElement(By.id("test2"))).selectByVisibleText(memorableWord.substring(index2, index2 + 1));
new Select(driver.findElement(By.id("test3"))).selectByVisibleText(memorableWord.substring(index3, index3 + 1));
}
public static int getNumber(String label)
{
// takes "Character 1", splits it into two parts, takes the second part (the number) and converts it to an int and returns it
return Integer.valueOf(label.split(" ")[1]);
}
}
关于java - 难忘信息的写作测试 - 随机字符(从 10 个中选择 3 个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35289242/