-3

I am trying to write a Selenium test against Amazon site. I want to get "Sign in" element so that I can click on it.

url: www.amazon.es

Here is my Selenium Code:

System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.es");

try
{           
    driver.findElement(By.id("nav-link-accountList")).click();
}
catch (Exception e)
{
    System.out.println("Not Found");
}

Sometimes the code works correctly but sometimes it does not find the ID "nav-link-yourAccount". What is the problem? and how can I solve it?

JeffC
  • 22,180
  • 5
  • 32
  • 55
mvillegas
  • 57
  • 2
  • 9
  • Add some implicit wait and try again – santhosh kumar Aug 18 '17 at 11:32
  • try to use some implicitWait `driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);` let me know if it works – NarendraR Aug 18 '17 at 11:32
  • You can also use Expected Conditions make the test wait for the button to become available https://stackoverflow.com/a/36596333/1501613 – cYrixmorten Aug 18 '17 at 11:36
  • I have added driver.manage (). Timeouts (). ImplicitlyWait (15, TimeUnit.SECONDS); As you indicated, but still not working. – mvillegas Aug 18 '17 at 11:40
  • @mvillegas any error you getting ? – NarendraR Aug 18 '17 at 11:45
  • what kind of Expected Conditions? I am starting with selenium webdriver and java – mvillegas Aug 18 '17 at 11:46
  • @Tuks Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: #nav\-link\-yourAccount – mvillegas Aug 18 '17 at 11:48
  • @mvillegas can you click on this element `driver.findElement(By.id("nav-link-accountList")).click();` I'm not seeing the id on page what you have mentioned – NarendraR Aug 18 '17 at 11:55
  • @Tuks Ok sorry, copy the code wrong. The correct id is that you indicates me but sometimes don't work fine Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: #nav\-link\-accountList – mvillegas Aug 18 '17 at 12:01
  • Run it in debug mode several times until you get the error then look at the page and see why it's failing. – JeffC Aug 18 '17 at 13:49

5 Answers5

0

The element which you are trying to click with id as nav-link-yourAccount is not clickable. To proceed further you need to click either the link with text Hola. Identifícate or the link with text Mi cuenta using one of the following xpaths:

//a[@id='nav-link-yourAccount']/span[text()='Hola. Identifícate']

or

//a[@id="nav-link-yourAccount"]/span[contains(text(),'Mi cuenta')]
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I wrote: try { driver.findElement(By.linkText("//a[@id=\"nav-link-yourAccount\"]/span[contains(text(),'Mi cuenta')]")).click(); }catch (Exception e) { e.printStackTrace(); System.out.println("Not Found"); } – mvillegas Aug 18 '17 at 13:39
  • and KO again with this error INFORMACIÓN: Detected dialect: W3C org.openqa.selenium.NoSuchElementException: Unable to locate element: //a[@id="nav-link-yourAccount"]/span[contains(text(),'Mi cuenta')] For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: 'unknown', revision: 'unknown', time: 'unknown' – mvillegas Aug 18 '17 at 13:39
0

Provide few seconds of wait, before click to this webelement so your driver may able to find the webelement.

For wait i am using Explicit wait method.

WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("nav-link-accountList"))));

driver.findElement(By.id("nav-link-accountList")).click();
Jainish Kapadia
  • 2,603
  • 5
  • 18
  • 29
0

Apply wait until element is appeared, so that it avoids NoSuchElementException and code is working without any error.

Below code is working fine:

driver.get("https://www.amazon.es");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement accontButton=driver.findElement(By.id("nav-link-accountList"));
WebDriverWait waitforelement=new WebDriverWait(driver,20);
waitforelement.until(ExpectedConditions.elementToBeClickable(accontButton));
try{           
accontButton.click();
}

catch (Exception e){
System.out.println("Not Found");
}
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
0

Instead using implicit wait try using explicit wait for the login element.

I've tried with explicit wait over 50 click and it did works.

Here is code you can use.

public class dump {
    public static void main(String a[]){
        System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        WebDriverWait wait = new WebDriverWait(driver, 15);
        for(int i=0; i<=50; i++){
            driver.get("https://www.amazon.es");
            try{    
                wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='nav-link-accountList']")));  
                driver.findElement(By.xpath("//*[@id='nav-link-accountList']")).click();
                System.out.println("clicked\t"+i);
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("Not Found");
            }
        }

    }
}

Here is the proof of run:

enter image description here

All the best!!

  • I pasted your code and it don't work fine. this error :INFORMACIÓN: Detected dialect: W3C org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //*[@id='nav-link-accountList'] (tried for 15 second(s) with 500 MILLISECONDS interval) – mvillegas Aug 18 '17 at 13:45
  • `wait.until()` returns the element waited for so you can do `wait.until(...).click()` and save some code along with not having to scrape the page twice. – JeffC Aug 18 '17 at 13:47
-1

Have you tried to find elements by xpath?

System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.es");

try
{           
        driver.findElement(By.xpath("//*[@id='nav-link-accountList']")).click();

}catch (Exception e)
 {
        System.out.println("Not Found");

    }
Justin Sp
  • 17
  • 2
  • I have tried with driver.findElement(By.xpath(".//*[@id='nav-link-accountList']")).click(); and it does not always work well. Sometimes OK and sometimes KO Unable to locate element: .//*[@id='nav-link-accountList'] – mvillegas Aug 18 '17 at 12:18
  • 2
    if id is there then why xpath ? any specific reason – NarendraR Aug 18 '17 at 12:21