3

I am running an automated test in selenium/intelliJ/Java. The webdriver is supposed to click the drop down menu on the Amazon nav bar and then click one of the links within the drop down menu. It does both these things correctly, the drop down option leads to its link, however the selenium test itself fails, here is the error:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"Full Shop Directory"}

and here is my code:

package com.testing.webdriver;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;


import java.util.Random;
import java.util.concurrent.TimeUnit;

public class MyFirstTest {
    WebDriver driver = new ChromeDriver();

    @BeforeClass
    public static void setupWebdriver() {
        WebDriverManager.chromedriver().setup();
    }

   
    private static final By SHOP_BY_DEPARTMENT = By.cssSelector("#nav-link-shopall");
    private static final By SHOP_ALL = By.cssSelector("#nav-flyout-shopAll > div.nav-template.nav-flyout-content.nav-tpl-itemList > a");
    


    @Test
    public void startWebdriver() {

        driver.navigate().to("https://www.amazon.co.uk/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        WebElement shopByDepartment = driver.findElement(SHOP_BY_DEPARTMENT);
        shopByDepartment.click();
        
        WebElement ShopAllNav = driver.findElement(By.linkText("Full Shop Directory"));
        ShopAllNav.click();

        Assert.assertTrue("matches current url",
                driver.getCurrentUrl().matches("https://www.amazon.co.uk/gp/site-directory/ref=nav_shopall_fullstore"));

    }

    @After
    public void breakdown() throws InterruptedException {
        Thread.sleep(20000);
        driver.close();
    }

The test should be passing as it does what I'm telling it. I assume it's something to do with the link being in the drop down menu, as the error says, but I still don't know how I would rectify this.

starball
  • 20,030
  • 7
  • 43
  • 238
golf umbrella
  • 233
  • 2
  • 4
  • 16

3 Answers3

1

What i have seen at amazon:

enter image description here

'full shop directory' is not (really a link text). if the element is <a>this is a text</a> then i consider it for link text.

In your case the text is formed with a-lot of white-spaces **********Full Shop Directory********** and this flaky and can cause issues (possible in your case).

Correct your locator to be struct-safe and try again.

Example: xpath: //a/span[@class='nav-text' and text()='Full Shop Directory']

Update: Your test is written wrong or no idea what is your actual goal.

What you do:

  1. click on nav menu and same view-page of 'full shop directory' is getting open. links are different.
  2. click on button from 'shop by department' (which is hidden)
  3. assert urls...

Suggested Steps:

  1. Move to 'shop by department' and trigger the dropdown.

  2. click on 'full shop directory' button from dropdown.

  3. wait for page to load

  4. assert page url.

Code:

driver.navigate().to("https://www.amazon.co.uk/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
WebElement shopByDepartment = driver.findElement(SHOP_BY_DEPARTMENT);

Actions ac = new Actions(driver);
ac.moveToElement(shopByDepartment).perform();

WebElement ShopAllNav = driver.findElement(By.linkText("Full Shop Directory"));
ShopAllNav.click();

Assert.assertTrue(driver.getCurrentUrl().matches("https://www.amazon.co.uk/gp/site-directory/ref=nav_shopall_fullstore"), "matches current url");

Code has been tested and confirmed as working.

Infern0
  • 2,565
  • 1
  • 8
  • 21
  • I've tried doing the same thing with a css selector instead (which didn't work) so I assume that's not the issue. – golf umbrella Jan 25 '19 at 11:39
  • are you sure that only 1 item is matching the locator? – Infern0 Jan 25 '19 at 11:41
  • I'm pretty sure yeah, the locator is up there is my code, the second private static final (SHOP_ALL), its pretty specific – golf umbrella Jan 25 '19 at 11:52
  • 1
    your test is written quite worng. You click on the nav menu and open 'full store' page. And then try to click on button which is located on the dropdown of SHOP_BY_DEPARTMENT. check the update on my answer – Infern0 Jan 25 '19 at 12:28
1

To expand the Dropdown Menu on the Amazon nav bar you don't need to click() rather Mouse Hover inducing WebDriverWait and you can use the following solution:

  • Code Block:

    System.setProperty("god.bless.you", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    options.addArguments("disable-infobars");
    options.addArguments("--disable-extensions"); 
    WebDriver driver =  new ChromeDriver(options);
    driver.get("https://www.amazon.co.uk/");
    new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#nav-shop>a#nav-link-shopall")))).perform();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.nav-catFlyout.nav-flyout div.nav-template.nav-flyout-content.nav-tpl-itemList a"))).click();
    Assert.assertTrue(driver.getCurrentUrl().matches("https://www.amazon.co.uk/gp/site-directory/ref=nav_shopall_fullstore"));
    driver.quit();
    
  • Console Output:

    Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387) on port 41299
    Only local connections are allowed.
    Jan 25, 2019 5:41:24 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: OSS
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You need to Wait for that particular component to be visible. After Its visibility then you can apply action on it.

You can try the code below.

WebElement ShopAllNav = driver.findElement(By.linkText("Full Shop Directory"));
WebDriverWait wait = new WebDriverWait(driver,200);
wait.until(ExpectedConditions.visibilityOf(ShopAllNav));
ShopAllNav.click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
sp324
  • 285
  • 2
  • 20