0

I am trying to automate login on facebook via selenium 3 and getting Null pointer exception for username field. I tried finding the webelement by "id" and "xpath" (you can see in commented code below ), it works when manually search in DOM but getting exception when run via automation code.

Below is my code :

 public class LoginClass {

    static WebDriver driver;

    /*@FindBy(xpath =    
         "//form[@id='login_form']/table/tbody/tr[2]/td/input[@id='email']")
    public WebElement Username;*/

    /*@FindBy(css = "input[id=email]")
        public WebElement Username;*/

       @FindBy(id = "email")
      public WebElement Username;


           @FindBy(xpath = 
          "//form[@id='login_form']/table/tbody/tr[2]/td[2]/input[@id='pass']")
            public WebElement Password; 

         @FindBy(id='pass']          
            public WebElement Password;

          @FindBy(id = "loginbutton")
         public WebElement Submit;

    void Login() throws InterruptedException
    {
        System.out.println("Enter Username Please: ");
        Scanner scan1 = new Scanner(System.in);
        String User = scan1.next();
        System.out.println("Enter Password Please: ");
        Scanner scan2 = new Scanner(System.in);
        String Pass = scan1.next();     


        Map<String, Object> chromeoptions = new HashMap<String, Object>();
        chromeoptions.put("binary", "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, chromeoptions);
        ChromeOptions options = new ChromeOptions();
        System.setProperty("webdriver.chrome.driver", "A:\\chromedriver.exe");
         driver = new ChromeDriver();
        driver.get("https://www.facebook.com");
        Thread.sleep(5000);

        Username.clear();
        Username.sendKeys(User);

        Password.clear();
        Password.sendKeys(Pass);

        Submit.click();


    }

    public static void main(String args[]) throws InterruptedException
    {

        LoginClass lc = new LoginClass();
        lc.Login(); 

    }

   }

But I am getting below error :

 org.openqa.selenium.remote.DesiredCapabilities chrome
INFO: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
Starting ChromeDriver 77.0.3865.40 (f484704e052e0b556f8030b65b953dce96503217-refs/branch-heads/3865@{#442}) on port 43346
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1570871808.414][WARNING]: Timed out connecting to Chrome, retrying...
Oct 12, 2019 2:16:50 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Exception in thread "main" java.lang.NullPointerException
    at facebookLogin.LoginClass.Login(LoginClass.java:56)
    at facebookLogin.LoginClass.main(LoginClass.java:71)

Not really sure what I am doing wrong, I have tried finding username by multiple ways like Id, xpath, name etc but getting null pointer exception

Please help

Aakash Goyal
  • 305
  • 1
  • 4
  • 22

0 Answers0