当前位置: 首页 > 知识库问答 >
问题:

组织。openqa。硒。NoSuchElementException无此类元素:无法定位元素

慕容灿
2023-03-14

我尝试使用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();
    }
}

注意:我尝试使用和xpath,但仍然不起作用。我也尝试在本文中使用一些解决方案,但没有结果

这是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个答案

姚鹤龄
2023-03-14
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();
 类似资料: