Home > Back-end >  Selenium test passes on hovering drop-down menu item click but does nothing
Selenium test passes on hovering drop-down menu item click but does nothing

Time:08-13

The website is https://cloudwise.nl/ I'm trying to click on Dit is Cloudwise > Alle Cludwisers with Selenium using Java. It's a hoverable drop-down menu so I saw people handle this situation with the help of waiting until the presence of Element Located functions. so that's my code piece:

Actions action = new Actions(driver);
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
        WebElement menu = wait.until(ExpectedConditions.presenceOfElementLocated((By.xpath("//a[@class='sf-with-ul'][contains(text(),'Dit is Cloudwise')]"))));
        WebElement submenu = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[@id='menu-item-6380']//a[contains(text(),'Alle Cloudwisers')]")));
        action.moveToElement(menu).moveToElement(driver.findElement(By.xpath("//li[@id='menu-item-6380']//a[contains(text(),'Alle Cloudwisers')]"))).click().build().perform();

But the test still passes and it does not click. What could I be doing wrong?

CodePudding user response:

To Mouse Hover on Dit is Cloudwise and then to click on Alle Cludwisers you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use the following locator strategies:

  • Using xpath:

    new Actions(driver).moveToElement(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='Dit is Cloudwise']")))).build().perform();
    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='Dit is Cloudwise']//following::ul[1]/li/a"))).click();
    

CodePudding user response:

Try changing presenceOfElementLocated with visibilityOfElementLocated.
Also, looks like

action.moveToElement(menu).moveToElement(driver.findElement(By.xpath("//li[@id='menu-item-6380']//a[contains(text(),'Alle Cloudwisers')]"))).click().build().perform();

Can be changed with

action.moveToElement(menu).moveToElement(submenu).click().build().perform();

CodePudding user response:

@Erthan Yumer, you can achieve this in the way your initial implementation with the slight change as given below:

Actions action = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement menu = wait.until(ExpectedConditions
        .presenceOfElementLocated((By.xpath("//a[@class='sf-with-ul'][contains(text(),'Dit is Cloudwise')]"))));
action.moveToElement(menu)
        .moveToElement(wait.until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("//li[@id='menu-item-6380']//a[contains(text(),'Alle Cloudwisers')]"))))
        .click().build().perform();
  • Related