【问题标题】:Selenium WebDriver : Login Script not workingSelenium WebDriver:登录脚本不起作用
【发布时间】:2024-04-20 23:50:01
【问题描述】:

我正在尝试创建一个登录脚本,我正在尝试验证登录是否成功。但是,它会抛出 NoSuchElementException。任何更正脚本的输入都将非常有帮助。

下面是我调用登录方法的页面类:

package abhi;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;

public class PageClass 
{

    //Declare the WebDriver
    WebDriver driver;
    //Declare the Login Page Elements
    By Username = By.name("username");
    By Password = By.name("password");
    By LoginButton = By.className("ui-button-text");
    By OnlineCatalog = By.linkText("Online Catalog");
    By ErrorMessage = By.className("messageStackError");

    //Create the constructor with the same name as that of the Page Class
    public PageClass (WebDriver driver)
    {
        this.driver=driver;
    }

    //Create Login Method

    public void Login (String Uname, String Pwd)           
    {
        driver.findElement(Username).sendKeys(Uname);
        driver.findElement(Password).sendKeys(Pwd);
        driver.findElement(LoginButton).click();            
    }
 }

下面是验证登录的测试脚本,我正在使用以下验证:

1)如果注销链接可用,则打印“登录成功” 2)如果显示错误消息,打印“登录失败” 但是,当我输入无效凭据时,它不会通过进入“else if”来读取错误消息,并且仍然会继续寻找注销链接,因此会抛出 NoSuchElementException。下面是测试脚本:

package abhi;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver

public class TestCaseClass 
{     
    public static WebDriver driver;

    public static void main(String[] args) throws InterruptedException    
    {
       System.setProperty("webdriver.chrome.driver", 
       "C:\\Users\\k746261\\Desktop\\Selenium\\chromedriver.exe");
       driver = new ChromeDriver();
       PageClass object = new PageClass (driver);

       driver.get("http://www.gcrit.com/build3/admin/login.php");
       object.Login("admin1", "admin@123");
       Thread.sleep(3000);

       if ((driver.findElement(By.linkText("Logoff")).isDisplayed()))
       {
          System.out.println("Login Successfull");
       }
       else  if (((driver.findElement(By.linkText("Logoff")).isDisplayed()==false)) 
        || ((driver.findElement(By.tagName("td")).isDisplayed())))
       {    
          System.out.println("Login Failed");
       }
       driver.close();
     }
 }

【问题讨论】:

  • 哪个元素通过异常

标签: java selenium-webdriver selenium-chromedriver login-script


【解决方案1】:

您的代码会抛出 NoSuchElementException,因为它在页面上找不到 Logoff 链接。您应该使用 try{} catch{} 块包装代码,以检查该元素是否是否显示。尝试像这样修改,看看是否有帮助:

Boolean elementvisible ;
try
{
    driver.findElement(By.linkText("Logoff"));
    elementvisible = true;
}
catch (NoSuchElementException e) {

// TODO: handle exception
   System.out.println("Element not found : " + e);
   elementvisible = false;
}

if (elementvisible == true)
{
    System.out.println("Login Successfull");
}
else  if ((elementvisible == false) || (driver.findElement(By.tagName("td")).isDisplayed()))
{    
    System.out.println("Login Failed");
}

注意:在正面测试用例(登录成功)的情况下,您可以添加WebDriverWait wait 等待链接可见。

【讨论】:

    最近更新 更多