0

I just want to hover over the "Departments" drop-down list on the Amazon website. The code looks alright but the list is not showing. It's the Department drop-down list I am trying to show

Here's my code

    driver = new ChromeDriver();
    driver.get("https://www.amazon.com");
    Actions actions = new Actions(driver);
    WebElement ele = driver.findElement(By.xpath("//span[@class='nav-line-2']"));
    Thread.sleep(300);
    actions.moveToElement(ele);
    actions.perform();
    actions.perform();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
squathub
  • 33
  • 1
  • 7

2 Answers2

1

Looks like the xpath is not unique and with the same locator, locating 6 elements in the page. When we have more than one element with same locator, selenium go for first element. In your case, unfortunately 'Departments' is not the first element with that locator.

Change your xpath to below: [Tested & worked]

//span[@class='nav-line-2' and contains(.,'Departments')]
Dhamo
  • 1,171
  • 3
  • 19
  • 41
  • Is this right WebElement ele = driver.findElement(By.xpath("//span[@class='nav-line-2' and contains(.,'Departments')]")); – squathub Oct 28 '18 at 18:27
  • whats the ., for in the Departments bracket? – squathub Oct 28 '18 at 18:31
  • Sorry it worked thanks, I copied your code thought mine was the same so I guess not so thanks Dhamo – squathub Oct 28 '18 at 18:42
  • It means text(), since the whole 'Department' text falls in span element we can use "." instead of "text()". You will not be able to use in few cases which are detailed [here](https://stackoverflow.com/questions/38240763/xpath-difference-between-dot-and-text) – Dhamo Oct 28 '18 at 18:42
0

To Mouse Hover over the element with text as Departments you need to induce WebDriverWait for the desired element to be visible and use moveToElement() method in conjunction with perform() method and you can use the following solution:

  • Code Block:

    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.chrome.ChromeOptions;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class amazon_com_Departments {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            WebDriver driver =  new ChromeDriver(options);
            driver.get("https://www.amazon.com");
            WebElement department = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@id='nav-link-shopall' and normalize-space()='Departments']")));
            new Actions(driver).moveToElement(department).perform();
        }
    }
    
  • Browser Snapshot:

amazon_hover_Department

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • How do you get the length of the list. I used the code: List e = driver.findElements(By.xpath("//span[@class='nav-line-2' and contains(.,'Departments')]")); int itemsCount = e.size(); System.out.println(itemsCount); – squathub Oct 29 '18 at 13:14
  • @squathub I am afraid you question was about **Hover over dropdown menu in Amazon** hence was my answer. If your requirement have changed can you raise a new request as per your new requirement? StackOverflow volunteers will be happy to help you out. – undetected Selenium Oct 29 '18 at 13:17
  • @squathub Upvote the answer if this/any answer is/was helpful to you for the benefit of the future readers. – undetected Selenium Nov 25 '18 at 17:51