I am just started to learn Selenium webdriver in an online course.
Since I am a beginner to both testing and Java programming so kindly help me out.
I was trying to run the automation code to fill the username automatically in Facebook Login page. Initial running the program had successful output.
But on the second attempt, I faced this below error
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"email"}
After that, I Googled and found this SO Question Tried to copy this answer in my code.
public class Helloworld {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Seenu\\Selenium\\Driver"
+"\\Chromedriver\\chromedriver.exe");
WebDriver drive = new ChromeDriver();
drive.get("https://www.facebook.com");
// part copied from other SO question
//Copied code starts here with little modification
List<WebElement> elements = drive.findElements(By.id("email"));
if(elements.size() > 0)
{
System.out.println(elements.get(0).getText());
}
//Copied code ends here.
else
{
elements.get(0).sendKeys("username@gmail.com");
System.out.println("Username successfully entered");
}
}
}
But,I am getting this below error.
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Basic.Helloworld.main(Helloworld.java:40)
I am aware of index out of bound error.
My question is why Selenium driver fails to recognize id element of Facebook login page
Can you guys please me out to solve this.