0

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?

Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26

3 Answers3

1

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();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Still got the same error: `Expected condition failed: waiting for element to be clickable: By.xpath: //a[text()='Alle Cloudwisers']//following::ul[1]/li/a (tried for 10 second(s) with 500 milliseconds interval)` – Erhan Yumer Aug 12 '22 at 14:38
  • Can we use `visibilityOfElementLocated.build().perform()` at any hoverable object? Like even at buttons need hovering to become active? – Erhan Yumer Aug 12 '22 at 19:02
  • Yes, but take care of the syntax. – undetected Selenium Aug 12 '22 at 19:04
0

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();
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • I did the changes you mentioned but still doesn't work. Now I get the error: `Expected condition failed: waiting for visibility of element located by By.xpath: //a[@class='sf-with-ul'][contains(text(),'Dit is Cloudwise')] (tried for 5 second(s) with 500 milliseconds interval)` – Erhan Yumer Aug 12 '22 at 14:07
  • Is the submenu opens by hovering over the menu element or user need to click on menu to open the submenu? – Prophet Aug 12 '22 at 14:32
  • 1
    Opens by hovering. These canges solved the problem: `new Actions(driver).moveToElement(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@class='sf-with-ul'][contains(text(),'Dit is Cloudwise')]")))).build().perform(); new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[@id='menu-item-6380']//a[contains(text(),'Alle Cloudwisers')]"))).click();` – Erhan Yumer Aug 12 '22 at 15:00
0

@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();
ketanvj
  • 511
  • 4
  • 5
  • Thanks for reply but using visibilityOfElementLocated method solved my problem. And this is the code piece I used: `new Actions(driver).moveToElement(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@class='sf-with-ul'][contains(text(),'Dit is Cloudwise')]")))).build().perform(); new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[@id='menu-item-6380']//a[contains(text(),'Alle Cloudwisers')]"))).click();` – Erhan Yumer Aug 12 '22 at 17:48