org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (x, y) is out of bounds while MouseHover with GeckoDriver Firefox Selenium

13,983

Solution 1

The main issue with the Web Application is that the HTML DOM attains document.readyState equals to complete even before the sub-menu element with text as Selenium WebDriver gets rendered. Hence you see the error as:

Exception in thread "main" org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (-4899, 91) is out of bounds of viewport width (1366) and height (664)

Solution

So an ideal solution would be:

  • Induce WebDriverwait for the titleIs() Execute Automation
  • Induce WebDriverwait for the menu element with text as Automation Tools
  • Induce WebDriverwait for the sub-menu element with text as Selenium
  • Induce WebDriverwait for the sub-menu elementToBeClickable with text as Selenium
  • You can use the following solution:
  • Code Block:

        import org.openqa.selenium.By;
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.firefox.FirefoxDriver;
        import org.openqa.selenium.interactions.Actions;
        import org.openqa.selenium.support.ui.ExpectedConditions;
        import org.openqa.selenium.support.ui.WebDriverWait;
    
        public class MouseHoverFirefox {
    
            public static void main(String[] args) {
    
                System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
                WebDriver driver=new FirefoxDriver();
                driver.get("http://www.executeautomation.com/demosite/index.html");
                new WebDriverWait(driver, 20).until(ExpectedConditions.titleIs("Execute Automation"));
                new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@id='Automation Tools']")))).build().perform();
                new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@class='active has-sub']/a/span//following::ul[1]/li[@class='has-sub']/a/span[@id='Selenium']")))).build().perform();
                new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@class='active has-sub']/a/span//following::ul[1]/li/a/span[@id='Selenium']//following::ul[1]/li/a/span[text()='Selenium WebDriver']"))).click();
            }
        }
    
  • Browser Snapshot:

selenium_WebDriver

Solution 2

Please try the below code(if you are inside frame , you need to come out and use below code):

WebDriver driver=new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,400)");
Share:
13,983
Michal Jeruzal
Author by

Michal Jeruzal

Updated on June 05, 2022

Comments

  • Michal Jeruzal
    Michal Jeruzal almost 2 years

    I am learning how to automate tests with Selenium WebDriver, however I got stuck and cannot make dropdown menu to work in Firefox. The same code runs perfectly fine in Chrome.

    The site I am practicing on is: http://www.executeautomation.com/demosite/index.html and I want to click the following item from menu: Automation Tools > Selenium > Selenium WebDriver.

    The error message suggest that the web element may not be loaded on the screen yet, so I have implemented some method to wait with every execution until the element shows up:

    public static void ImplicitWait(WebDriver driver){
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    }
    

    but it did not helped.

    Then I read that it is better to "pipe" those moveToElement() methods instead of performing them one by one. So I changed this:

    action.moveToElement(menu).perform();
    action.moveToElement(selenium).perform();
    action.moveToElement(seleniumWebDriver).click().build().perform();
    

    to one line. At this point it started to work on Chrome, but I am still struggling to make it work on Firefox.

    The current code looks like this:

    System.setProperty("webdriver.gecko.driver", "C:\\Drivers\\geckodriver-v0.24.0-win64\\geckodriver.exe");
    System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
    WebDriver driver = new FirefoxDriver();
    
    ImplicitWait(driver);
    
    driver.navigate().to("http://executeautomation.com/demosite/index.html");
    
    WebElement menu = driver.findElement(By.id("Automation Tools"));
    WebElement selenium = driver.findElement(By.id("Selenium"));
    WebElement seleniumWebDriver = driver.findElement(By.id("Selenium WebDriver"));
    
    Actions action = new Actions(driver);
    action.moveToElement(menu).moveToElement(selenium).moveToElement(seleniumWebDriver).click().build().perform();
    

    As I mentioned above the same works fine when I switch to Chrome, but with Firefox I get the error message:

    Exception in thread "main" org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (-9862, 206) is out of bounds of viewport width (1283) and height (699)
    

    I am using: * Firefox v66.0.2 * Java v1.8.0_201 * Selenium Java v3.141.59 * GeckoDriver v0.24.0

    Please help.

  • Michal Jeruzal
    Michal Jeruzal about 5 years
    Tried it, but the same issue still persists. Also, your code is missing "action." before the method moveToElement.
  • Michal Jeruzal
    Michal Jeruzal about 5 years
    It works with Firefox given the mouse pointer is out of the web browser but it does not work in Chrome anymore. It opens menu and submenu but do not click the "Selenium WebDriver" web element. Also, I do not understand why you used clickAndHold() method instead of click(). What is the difference? I imagine that it will "press and hold" the LMB, move the coursor and release LMB. If I try to do that manually in the browser the dropdown menu would not appear, hence I am confused.
  • Michal Jeruzal
    Michal Jeruzal about 5 years
    It works with Firefox given the mouse pointer is out of the web browser, but it does not work in Chrome anymore. It opens menu "Automations Tools" and goes to submenu "BDD" instead. I have tried to search for elements using By.xpath as well but with no luck.
  • Michal Jeruzal
    Michal Jeruzal about 5 years
    Same situation as with @kajal-kundu 's code. It works with Firefox given the mouse pointer is out of the web browser, but it does not work in Chrome anymore. It opens menu "Automations Tools", but goes to submenu "BDD" instead.
  • undetected Selenium
    undetected Selenium about 5 years
    What do you mean by @kajal-kundu 's code ? What exacatly do you mean by mouse pointer is out of the web browser ?? You specifically mentioned same works fine when I switch to Chrome, but with Firefox I get the error. This answer is specifically for Firefox . Can you just copy-paste the code and execute and update me the status please ???
  • Michal Jeruzal
    Michal Jeruzal about 5 years
    Kajal Kundu is the person that also answered to my question. If mouse cursor is over the web browser window the provided solution will not work. I have executed your code, it works in Firefox (given the cursor is not over web browser), but it does not work with Chrome anymore, hence I cannot accept the answer. I don't want to create multiple identical methods but for different web browsers if it is not necessary.
  • undetected Selenium
    undetected Selenium about 5 years
    I don't think Kajal Kundu is associated with this answer anyway. I have no idea what exactly you mean by mouse cursor is over the web browser window as most of we developers will trigger the execution from an IDE e.g. Eclipse. To reiterate, you specifically mentioned same works fine when I switch to Chrome, but with Firefox I get the error. Though this answer is generic but works perfecto specifically with Firefox. Am I missing something?
  • KunduK
    KunduK about 5 years
    My output comes from Chrome only and you are saying it is not working with Chrome.Sorry I can’t help you.
  • Michal Jeruzal
    Michal Jeruzal about 5 years
    Strange thing, it started to work properly with your code on both web browsers after the reboot. Thank you :)