1

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.

Srini
  • 489
  • 1
  • 11
  • 25
  • You could try sleeping after the drive.get. Maybe there is some JS creating the input you are looking for. The element seems to exist in the page's source code directly, but maybe it gets deleted and recreated by facebook's frontend code when JS is detected. – antoineMoPa Dec 10 '17 at 18:15
  • 1
    Facebook doesn't allow you to scrape them so don't do it. Use the API. – WizKid Dec 10 '17 at 23:36
  • @antoineMoPa I tried your suggestion sorry It is not working for me.Thank you – Srini Dec 11 '17 at 16:55
  • @WizKid I am a Beginner so please explain me in elaborate manner. – Srini Dec 11 '17 at 16:57
  • 1
    Don't use Selenium. Use the API. Documentation about the API you can find at developers.facebook.com/docs – WizKid Dec 11 '17 at 17:20

2 Answers2

1

To login into Facebook you can use the following code block :

System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
WebDriver driver =  new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.findElement(By.cssSelector("input#email")).sendKeys("Selenium");
driver.findElement(By.cssSelector("input[name='pass']")).sendKeys("Automation");
driver.findElement(By.cssSelector("input[value='Log In']")).click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thanks you ,it works fine without any error.But why I cant able use ID element. – Srini Dec 11 '17 at 16:33
  • 1
    @Srini We have taken help of css to mock the DOM so that our solution remains intact incase of any change in the HTML. On the other hand as Facebook is ReactJS based `id` may not always get resolved properly. Please Accept the Answer. – undetected Selenium Dec 11 '17 at 18:15
0

Your code has some logical error. I tried with your code solving the logical error worked fine again and again in my PC.

Update your code as below

driver.get("http://www.facebook.com");  


List<WebElement> elements = driver.findElements(By.id("email"));     
if(elements.size() > 0)//
{
      System.out.println(elements.get(0).getText());
      elements.get(0).sendKeys("username@gmail.com");
      System.out.println("Username successfully entered");
 }
Mahmud Riad
  • 1,169
  • 1
  • 8
  • 19