org.openqa.selenium.NoSuchElementException没有这样的元素:无法找到元素

时间:2019-10-28 09:42:53

标签: selenium

我尝试使用selenium测试登录功能,但出现此异常:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@name='Guichet']"}

这是我的代码:

public class LoginTest {

    public static WebDriver driver; 

    @BeforeClass
    public void testSetup() {
        System.setProperty("webdriver.chrome.driver", "C:\\driver\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        //driver.manage().window().maximize();

    }

    @BeforeMethod
    public void openBrowser() {
        driver.get("http:url");
        System.out.println("We are currently on the following URL" +driver.getCurrentUrl());
    }

    @Test(description="This method validates the login functionality")
    public void loginFunction() {
        //WebDriverWait gui = new WebDriverWait(driver,100);

        //Locating the email field element via Name tag and storing it in the webelement
        WebElement guichet = driver.findElement(By.xpath("//input[@name='Guichet']"));
        WebElement matricule = driver.findElement(By.name("Matricule"));
        WebElement password = driver.findElement(By.name("Mot_de_passe"));
        WebElement save = driver.findElement(By.name("Valider"));
        //Entering text into the email field
        guichet.sendKeys("0000");

        matricule.sendKeys("login");
        password.sendKeys("password");
        save.click();

    }
    @AfterMethod
    public void postLogin(){
        System.out.println(driver.getCurrentUrl());
        Assert.assertEquals(driver.getCurrentUrl(), "http:url.html");
    }

    @AfterClass
    public void afterClass(){
        //driver.quit();
    }
}

注意::我尝试使用and xpath但仍然无法正常工作,我尝试在此Post中使用某些解决方案,但没有结果

这是HTML页面,使用菜单框架和其他表单登录页面

  

SCT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META name="GENERATOR" content="">
<META http-equiv="Content-Style-Type" content="text/css">
<TITLE> 
</TITLE>
<LINK href="Master.css" rel="stylesheet" type="text/css">
</HEAD>
<FRAMESET rows="14%,78%" frameborder="NO" border="0">
  <FRAME src="menu.html" scrolling="NO" name="entete">
  <FRAME src="SCT.html">
  <NOFRAMES>
  <BODY BGCOLOR="#FFFFFF">
  <P>L'affichage de cette page requiert un navigateur prenant en charge les
  cadres.</P>
  </BODY>
  </NOFRAMES>
</FRAMESET>
</HTML>

1 个答案:

答案 0 :(得分:1)

driver.switchTo().frame(0);
WebElement guichet = driver.findElement(By.xpath("//input[@name='Guichet']"));
WebElement matricule = driver.findElement(By.name("Matricule"));
WebElement password = driver.findElement(By.name("Mot_de_passe"));
WebElement save = driver.findElement(By.name("Valider"));
//Entering text into the email field
guichet.sendKeys("0000");

matricule.sendKeys("login");
password.sendKeys("password");
save.click();
driver.switchTo().defaultContent();