user9645057
user9645057

Reputation:

selenium not able to find Elements every time gives me exception

Following is my HTML Code :

<div class="dropdown">
    <button class="dropdown-toggle" type="button" id="dropdownMenu14" 
        data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
    <ul class="dropdown-menu" aria-labelledby="dropdownMenu14">
        <li><a href="#" data-toggle="modal" data-target="#LoginModel">Login</a></li>
    </ul>

And Following is my selenium code where I am getting Null Pointer Exception every time.

public void test3() throws Exception {
   // System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
   driver.get(baseUrl);
   driver.findElement(By.xpath("//button[@id='dropdownMenu14'])")).click();
   //driver.findElement(By.id("dropdownMenu14")).click();

   driver.findElement(By.id("create_login")).click();
   driver.findElement(By.name("username")).click();
   driver.findElement(By.name("username")).clear();
   driver.findElement(By.name("username")).sendKeys("Jahnvi");
   driver.findElement(By.name("password")).clear();
   driver.findElement(By.name("password")).sendKeys("123456");
   driver.findElement(By.id("check_login")).submit();
   driver.findElement(By.linkText("Login")).click();
   driver.findElement(By.xpath("(//input[@value='Login Your Account'])[2]")).click();
   driver.findElement(By.id("dropdownMenu14")).click();
   driver.findElement(By.id("logout_button")).click();
}

Upvotes: 1

Views: 262

Answers (2)

Ivan Van Dessel
Ivan Van Dessel

Reputation: 26

If there is multiple HTML tags with id="create_login" then selenium will return that it can not find the element. To debug the null reference exception we would need more information like the HTML DOM of the complete page, and what release of selenium webdriver you are using.

Upvotes: 1

user9645057
user9645057

Reputation:

I tried following code and it worked, I tried xpath instead of id,name etc. Can please anyone explain why it worked with xpath but not with id.

@Test
public void test3() throws Exception {

    driver.get(baseUrl);
    driver.findElement(By.xpath("//*[@id=\"dropdownMenu14\"]")).click();
    //driver.findElement(By.id("dropdownMenu14")).click();
    //driver.quit();
    driver.findElement(By.xpath("//*[@id=\"header\"]/section[1]/div/div/div[2]/div/ul/li[1]/div/div[1]/ul/li/a")).click();
    //driver.findElement(By.xpath("//*[@id=\"check_login\"]/div[1]/input")).click();
    //driver.findElement(By.xpath("//*[@id=\"check_login\"]/div[1]/input")).clear();
    driver.findElement(By.xpath("//*[@id=\"check_login\"]/div[1]/input")).sendKeys("Jahnvi");
    // driver.findElement(By.xpath("//*[@id=\"check_login\"]/div[2]/input")).clear();
    driver.findElement(By.xpath("//*[@id=\"check_login\"]/div[2]/input")).sendKeys("123456");
    driver.findElement(By.xpath("//*[@id=\"check_login\"]/div[4]/input")).submit();
    driver.findElement(By.xpath("(//input[@value='Login Your Account'])[2]")).click();
    //driver.findElement(By.xpath("//*[@id=\"dropdownMenu14\"]")).click();
}

Upvotes: 0

Related Questions