我正在学习Selenium Webdriver,正在编写代码以填写rediff.com的注册表格。选择rediffmail ID时,有一个验证按钮,用于检查输入ID的可用性,并在选择的ID可用时给出结果。我想比较该文本,如果ID可用,那么它将自动填充页面的其余部分,但是如果不可用,它将停止并给出一条消息以选择新的ID。我能够实现一些代码,但是我不认为这是最好的方法,因此请专家咨询。请建议我在下面添加了我的代码。先谢谢您的帮助。public void fillformredifflocators() { try { invokebrowser("http://register.rediff.com/register/register.php?FormName=user_details"); driver.findElement(By.xpath("/html/body/center/form/div/table[2]/tbody/tr[3]/td[3]/input")).sendKeys("Rediff User"); driver.findElement(By.xpath("/html/body/center/form/div/table[2]/tbody/tr[7]/td[3]/input[1]")).sendKeys("abcd540"); driver.findElement(By.className("btn_checkavail")).click(); driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS); String expectedMessage = "Yippie! The ID you've chosen is available."; String Message = driver.findElement(By.xpath("//b[contains(text(),\"Yippie! The ID you've chosen is available.\")]")).getText(); Assert.assertEquals(Message, expectedMessage); if (expectedMessage.equals(Message)) { System.out.println("Congrats ! Your chosen id can be used"); } else { System.out.println("Please use a different id as the chosen id is taken"); } driver.findElement(By.xpath("/html[1]/body[1]/center[1]/form[1]/div[1]/table[2]/tbody[1]/tr[9]/td[3]/input[1]")).sendKeys("password123"); } catch (Exception e) { e.printStackTrace(); }}
1 回答

慕尼黑的夜晚无繁华
TA贡献1864条经验 获得超6个赞
您必须改进脚本中使用的定位器。尝试使用相对xpath而不是绝对xpath。而且您不必存储消息,然后在此处进行比较以进行验证。只需检查是否Yippie! The ID you've chosen is available.存在带有文本的元素,就可以将其本身视为验证。
if (driver.findElements(By.xpath("//b[contains(text(),\"Yippie! The ID you've chosen is available.\")]")).size()>0){
System.out.println("Congrats ! Your chosen id can be used");
}else{
System.out.println("Please use a different id as the chosen id is taken");
}
添加回答
举报
0/150
提交
取消