Search code examples
javahtmlselenium-webdriveriframewebautomation

Unable to locate element exception - Java Selenium


I'm getting an Unable to locate element exception, for reasons beyond me.

Here's the website: https://bo.competentfinman.com:1467/capexweb/capexweb/

And here's my code:

package artifactidshilpipackage;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import io.github.bonigarcia.wdm.WebDriverManager;

public class shilpi {

    public static void main(String[] args) throws Throwable {
        WebDriver driver;
        WebDriverManager.firefoxdriver().setup();
        driver = new FirefoxDriver();
        driver.get("https://bo.competentfinman.com:1467/capexweb/capexweb/");
        Thread.sleep(3000);
        driver.switchTo().frame("main");
        driver.findElement(By.id("dfuserid")).sendKeys("***");
        Thread.sleep(5000);
        driver.findElement(By.name("dfpassword")).sendKeys("***");
        Thread.sleep(5000);
        driver.findElement(By.name("B1")).click();
        Thread.sleep(5000);
        driver.findElement(By.xpath("/html/body/div[1]/a")).click();
        Thread.sleep(5000);
        driver.findElement(By.xpath("/html/body/table[2]/tbody/tr/td/input")).click();
        Thread.sleep(5000);
        driver.findElement(By.name("topsegment")).click();
        Thread.sleep(5000);
        Select dropdown = new Select(driver.findElement(By.name("topsegment")));
        dropdown.selectByValue("F&O");
        driver.findElement(By.name("nodeIcon3")).click();
        Thread.sleep(5000);
        driver.findElement(By.id("itemTextLink5")).click();
        Thread.sleep(5000);
        driver.findElement(By.name("B1")).click();
        // String a
        // driver.findElement(By.xpath("/html/body/table[2]/tbody/tr[108]/td[2]/b")).getText();
        // int b = Integer.parseInt(a);
    }
}

I had the same problem when I tried to use nested frames on someone's recommendation. The nested frame didn't work. But I'm on the correct frame I believe...


Solution

  • The below line is incorrect:

    driver.findElement(By.id("dfuserid")).sendKeys("***");
    

    Notice the HTML below. It is the @name attribute which we need to use and not @id.

    Enter image description here

    So the code should be:

    driver.findElement(By.name("dfuserid")).sendKeys("***");