Search code examples
javaseleniumselenium-webdriveractionwebdriverwait

Hover over dropdown menu in Amazon


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();

Solution

  • 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