0

I am trying to run the below selenium code and I am getting an exception:

package demos;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class AmazonLogin {

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


        WebDriver driver;
        // Go to website and login


        driver = utilites.DriverFactor.open("chrome");
        driver.get("https://www.amazon.in/your-account");
        WebElement loginName = driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[2]/div[2]/a/div/div"));
        WebElement emailId = driver.findElement(By.xpath("//*[@id=\"ap_email\"]"));
        //  WebElement continueButton=driver.findElement(By.id("continue"));
        //  WebElement password=driver.findElement(By.name("password"));
        //  WebElement loginButton=driver.findElement(By.id("signInSubmit"));
        //  WebElement message=driver.findElement(By.className("nav-line-1"));
        //
        loginName.click();
        emailId.sendKeys("aryan.ragavan@gmail.com");
    }
}

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element:
Unable to locate element: {"method":"xpath","selector":"//*[@id="ap_email"]"}

Selenium is trying to find webelement emailid before it clicks loginName webelement. Please help.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

3 Answers3

0

This is because you are trying to read the emailId field before you have clicked on login. Move the following line below loginName.click() statement.

    WebElement emailId = driver.findElement(By.xpath("//*[@id=\"ap_email\"]"));
curlyBraces
  • 1,095
  • 8
  • 12
0

the issue is you are searching for 'app_element' on the main page of amazon.

Introduce yourself to POM implementation here:

https://www.guru99.com/page-object-model-pom-page-factory-in-selenium-ultimate-guide.html

DO-NOT use absolute xpath in any case as you did here:

findElement(By.xpath("/html/body/div[1]/div[2]/div/div[2]/div[2]/a/div/div"));

Here is an example of login logic for amazon:

@Test
public void amazon_login() {
    browseToUrl("https://www.amazon.in/your-account");

    // select login & security option
    WebElement loginAndSecurityBtn = driver.findElement(By.xpath("//div[@data-card-identifier='SignInAndSecurity']"));
    // navigate to the next page
    loginAndSecurityBtn.click();

    // enter email or phone
    WebElement emailOrPhoneInput = driver.findElement(By.id("ap_email"));
    emailOrPhoneInput.sendKeys("example@email.com");

    // click continue btn
    WebElement continueBtn = driver.findElement(By.id("continue"));
    continueBtn.click();

    // enter password
    WebElement passwordInput = driver.findElement(By.id("ap_password"));
    passwordInput.sendKeys("password");

    // login
    WebElement loginBtn = driver.findElement(By.id("signInSubmit"));
    loginBtn.click();
}
Infern0
  • 2,565
  • 1
  • 8
  • 21
0

For Amazon Login, You Can Refer This Code . or Below POM(PageobjectModel) Code

    package TestId;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

public class NewTest {

    WebDriver driver;
  @Test
  public void f() 
  {
      System.setProperty("webdriver.chrome.driver", "E:\\New Folder\\chromedriver.exe");

      driver = new ChromeDriver();

      driver.manage().window().maximize();

      driver.get("https://www.amazon.in/your-account");

      WebElement e1 = driver.findElement(By.xpath("//*[@id='nav-link-yourAccount']/span[2]"));
      WebElement e2 = driver.findElement(By.xpath("//*[@id='nav-flyout-ya-signin']/a/span"));
      Actions a1 = new Actions(driver);
      a1.moveToElement(e1).click(e2).build().perform();
  }

  @AfterTest
  public void CheckLogin()
  {

      WebElement e3 = driver.findElement(By.xpath("//*[@id='authportal-main-section']/div[2]/div/div[1]/form/div/div/div"));

      WebDriverWait wait = new WebDriverWait(driver, 5);
      WebElement e4 = wait.until(ExpectedConditions.visibilityOf(e3));

      if(e4.isDisplayed())
      {
          driver.findElement(By.id("ap_email")).sendKeys("Test@gmail.com");

          System.out.println("Email Successfully Passed");
      } 
  }
}

I Used Webdriver Wait because, i Need to Wait Until The Login Page is Loaded and i Used an if-Condition because after The Page is Displayed Pass The Email id.

if You Find Difficultly in Above Use Use Below POM Method.

Amazon Login Using POM:

    package POM;

import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Pageobject 
{
    WebDriver driver;

    public Pageobject(WebDriver driver)
    {
        this.driver=driver;
    }

    @FindBy(xpath = "//*[@id='nav-link-yourAccount']/span[2]")
    public WebElement Siginpath;

    @FindBy(how=How.XPATH,using="//*[@id='nav-flyout-ya-signin']/a/span")
    public WebElement clicksignin;

    @FindBy(how=How.XPATH,using="//*[@id='authportal-main-section']/div[2]/div/div[1]/form/div/div/div")
    public WebElement ElementtobeVisible;

    @FindBy(how=How.ID,using="ap_email")
    public WebElement Email;


    public void Loginpage(String email)
    {
        Actions a1 = new Actions(driver);
          a1.moveToElement(Siginpath).click(clicksignin).build().perform();

          WebDriverWait wait = new WebDriverWait(driver, 5);
          WebElement e4 = wait.until(ExpectedConditions.visibilityOf(ElementtobeVisible));

          if(e4.isDisplayed())
          {
              Email.sendKeys(email);
          }  
    }

}

Test Case For POM :

    package POMtestcase;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;

import POM.Pageobject;

public class Pomtest 
{
    WebDriver driver;

    @Test
    public void Checkvaliduser()
    {
        System.setProperty("webdriver.chrome.driver", "E:\\New Folder\\chromedriver.exe");

          driver = new ChromeDriver();

          driver.manage().window().maximize();

          driver.get("https://www.amazon.in/your-account");

         Pageobject object = PageFactory.initElements(driver, Pageobject.class); //Calling The Class Here

         object.Loginpage("test@gmail.com");//Passing the Mail id


    }

}
koushick
  • 497
  • 2
  • 8
  • 30