0

I have created and executed the MouseHover functionality on the ADD-ONS tab within https://www.spicejet.com/ but it's not working with the below code.

Can anyone suggest what is missing in the code?

package SeleniumExamples;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class MouseHoverConcept {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","./driver/chromedriver81.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
        driver.get("https://www.spicejet.com/");
        // create object for mouse hover
        Actions action = new Actions(driver);
        action.moveToElement(driver.findElement(By.xpath("//*[@id=\"highlight-addons\"]"))).build().perform();
        Thread.sleep(3000);
        driver.findElement(By.linkText("SpiceMax")).click();
        //driver.quit();
    }
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

4 Answers4

1

To demonstrate the Mouse Hover functionality using Selenium on the ADD-ONS tab within https://www.spicejet.com/ you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use the following Locator Strategy:

  • Code Block:

    import java.util.Collections;
    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 mouseHover_spicejet_addons {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--start-maximized");
            options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
            options.setExperimentalOption("useAutomationExtension", false);
            WebDriver driver =  new ChromeDriver(options);
            driver.get("https://www.spicejet.com/");
            WebElement addons = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a#highlight-addons")));
            new Actions(driver).moveToElement(addons).build().perform();
            new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class='add-ons-tab']//li/a[contains(., 'SpiceMax')]"))).click();
        }
    }
    
  • Browser Snapshot

mousehover

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

did you try write it without "build"? :-) If it will not work, are you sure, that xpath is correct?

Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//*[@id=\"highlight-addons\"]")));
action.perform();
NatalSnowyFox
  • 66
  • 1
  • 3
0

Everything is correct. just add the sleep of 10 seconds after the get method like below

     driver.get("https://www.spicejet.com/");
     driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
     Thread.sleep(10000);


     // create object for mouse hover
        Actions action = new Actions(driver);
        action.moveToElement(driver.findElement(By.xpath("//a[@id='highlight-addons']"))).build().perform();
        Thread.sleep(3000);
        driver.findElement(By.linkText("SpiceMax")).click();
        //driver.quit();
UnknownBeast
  • 979
  • 1
  • 6
  • 13
0

mouseHover is inconsistent in Chrome. You can try JavaScript Executor instead. Below is the code snippet in Java:

WebElement elementToHover = driver.findElement(By.xpath("//*[@id='highlight-addons']"));
String mouseHoverScript = "if(document.createEvent)" +
                "{var evObj = document.createEvent('MouseEvents');" +
                "evObj.initEvent('mouseover', true, false); " +
                "arguments[0].dispatchEvent(evObj);} " +
                "else if(document.createEventObject) " +
                "{ arguments[0].fireEvent('onmouseover');}";
((JavascriptExecutor) driver).executeScript(mouseHoverScript, elementToHover);