简体   繁体   中英

Amazon login and logout scenario using selenium webdriver

I am new to selenium. I am trying to automate amazon login and log out. I am facing couple of problems.

1.In login page the button "continue" appears after username when I try to login for first time. But later when I try to login for second time , its does not appear. How to handle this. Here is the code I have written so far:

public void logindetails()
{

        Datafile d=new Datafile("C:\\Users\\kirruPC\\selenium divers\\Data.xlsx",0);

        String uname= d.username(0, 0);
        WebElement u=driver.findElement(useid);
        u.sendKeys(uname);
        u.click();



        if(driver.findElement(By.xpath(".//*[@id='continue']")).isDisplayed()==true)
        {
            driver.findElement(By.id("continue")).click();
            String psw=d.pass(0,1);
            driver.findElement(password).sendKeys(psw);
        }
        else
        {
            String psw=d.pass(0,1);
            driver.findElement(password).sendKeys(psw);
        }
}
  1. Unable to locate sign Out element.

Below is the code I have written to move to sign out link and click:

     public void logout() throws Exception
     {

          Actions a= new Actions(driver);
          WebElement ele=driver.findElement(By.xpath(".//*[@id='nav-link-accountList']"));
          a.moveToElement(ele).build().perform();



          driver.findElement(By.xpath(".//*[@id='nav-al-your-account']"));
          Thread.sleep(3000);
          driver.findElement(By.xpath(".//*[@id='nav-al-your-account']/a[22]")).click();
     }

Please help me

Thanks in advance

First of all, there is a high change that the element you are searching for might not exist at all in the page, so first let's check if the element exist (if the element does not exist, an exception is thrown, so let's add a handle for that as well). For all that, create a function like so:

public bool ElementExists(By locator)
{
    try
    {
        driver.findElement(locator); 
        //If no exception is thrown here, element exists, so return true
        return true; 
    }
    catch (NoSuchElementException ex)
    {
        return false;
    {
}

Now that we have a function that can safely check if an element exists without getting an exception, you can use it to determine if you will run the code handling the element. Function isDisplayed() already returns a bool, so checking equality with true is not necessary.

if(ElementExists(By.xpath(...)).isDisplayed())
{
    if(driver.findElement(By.xpath(".//*[@id='continue']")).isDisplayed())
    {
        driver.findElement(By.id("continue")).click();
    }
}
//The code below will run either way, so move it out of the if statement
String psw=d.pass(0,1);
driver.findElement(password).sendKeys(psw);

As for the second part of your question, your code can be simplified by just, searching for the element and then clicking it like so:

driver.findElement(By.xpath(".//*[@id='nav-al-your-account']/a[22]")).click();

If you will, double-check the xpath or "catch" the element by an ID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM