【问题标题】:How to check the username is valid如何检查用户名是否有效
【发布时间】:2017-10-04 22:00:12
【问题描述】:

实际上我尝试在登录页面中检查用户名和密码是否正确。 所以我编写了即将登录的代码。当注销按钮可见时检查用户是否有效。

但我的说法无效

driver.findElement(By.id("username")).sendKeys(sUsername);
driver.findElement(By.id("password")).sendKeys(sPassword);
driver.findElement(By.name("login")).click();


boolean logvisibile = driver.findElement(By.xpath(".//*[@id='account']/ul/li[2]/a")).isDisplayed();


Thread.sleep(1000);

if(logvisibile == true)

{
System.out.println("Succesfull");

}else

{
System.out.println("Failed");

}
}

**输出:

失败:测试(“testuser_1”,“Test@123”) org.openqa.selenium.NoSuchElementException:无法定位元素: {“method”:”xpath”,”selector”:”.//*[@id='account']/ul/li[2]/a”} 命令 持续时间或超时:10.06 秒有关此错误的文档, 请访问:http://seleniumhq.org/exceptions/no_such_element.html**

【问题讨论】:

    标签: selenium-webdriver testng


    【解决方案1】:

    我认为您甚至在单击登录之前检查了 LogOut 按钮的可见性。 (假设//*[@id='account']/ul/li[2]/a 是您的注销按钮)

    boolean logvisibile = driver.findElement(By.xpath(".//*[@id='account']/ul/li[2]/a")).isDisplayed();
    driver.findElement(By.name("login")).click();
    

    首先点击登录,然后检查可见性。

    driver.findElement(By.name("login")).click();
    boolean logvisibile = driver.findElement(By.xpath(".//*[@id='account']/ul/li[2]/a")).isDisplayed();
    

    也不需要这样做 - if(logvisibile == true) 你可以简单地做 if(logvisible)

    编辑:检查这个解决方案的登录场景,工作得很好。

        public static void main(String[] args) {
    
        driver = new FirefoxDriver();
        driver.navigate().to("http://newtours.demoaut.com/mercurysignon.php");
    
        WebElement username = driver.findElement(By.cssSelector("input[name='userName']"));
        username.sendKeys("dharam111");
    
        WebElement password = driver.findElement(By.cssSelector("input[name='password']"));
        password.sendKeys("password");
    
        WebElement loginBtn = driver.findElement(By.cssSelector("input[name='login']"));
        loginBtn.click();
    
        List<WebElement> objLogOutLink = driver.findElements(By.cssSelector("a[href*='signoff']"));
        if(objLogOutLink.size() > 0){
            if(objLogOutLink.get(0).getText().contains("SIGN-OFF")){
                System.out.println("Login successful...Success");
            }
        }
        else{
            System.out.println("Login Failed.");
        }
    
    }
    

    【讨论】:

    • 感谢您的帮助.. 仍然显示错误,例如“无法定位元素:{“method”:“xpath”,“selector”:”。//*[@id='account'] /ul/li[2]/a"} 命令持续时间或超时:10.40 秒...." 仍然如果循环未执行 也正在输入无效的用户名进行检查
    • 你能贴出你的注销按钮的 HTML sn-p 吗?
    • 你也可以试试这个来检查可见性 - WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='account']/ul/li[2]/a")));
    • 'html lang="en">
        '
      • 退出按钮在登录页面中不可见,并且没有进入主页。(因为我的用户名和密码无效)。所以总是显示“无法定位元素。我怎么能得到正确的输出。另外,如果循环不工作
      猜你喜欢
      相关资源
      最近更新 更多
      热门标签