org.openqa.selenium.interactions.MoveTargetOutOfBoundsException:(x,y)超出范围,而MouseHover和GeckoDriver Firefox Selenium [英] org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (x, y) is out of bounds while MouseHover with GeckoDriver Firefox Selenium

查看:329
本文介绍了org.openqa.selenium.interactions.MoveTargetOutOfBoundsException:(x,y)超出范围,而MouseHover和GeckoDriver Firefox Selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用Selenium WebDriver自动化测试,但是我陷入了困境,无法使下拉菜单在Firefox中工作.相同的代码在Chrome中运行得很好.

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.

我正在练习的站点是: http://www.executeautomation.com/demosite/index.html 我想从菜单中单击以下项目:自动化工具> Selenium> Selenium WebDriver.

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.

该错误消息表明Web元素可能尚未加载到屏幕上,因此我实现了一些方法来等待每次执行,直到该元素出现:

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

但没有帮助.

然后,我读到最好将这些moveToElement()方法管道化",而不是一个一个地执行它们.所以我改变了这个:

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

一行.此时,它开始在Chrome上运行,但我仍在努力使其在Firefox上运行.

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

当前代码如下:

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

正如我上面提到的,当我切换到Chrome时,同样可以正常工作,但是使用Firefox时,我会收到错误消息:

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)

我正在使用: * Firefox v66.0.2 * Java v1.8.0_201 * Selenium Java v3.141.59 * GeckoDriver v0.24.0

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

请帮助.

推荐答案

的主要问题Web应用程序 HTML DOM 获得的document.readyState等于 complete ,甚至在呈现文本为 Selenium WebDriver 的子菜单元素之前也是如此.因此,您看到的错误为:

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:

  • titleIs() Execute Automation
  • 生成 WebDriverwait
  • WebDriverwait 用作菜单元素,其文本为自动化工具
  • 为文本文本为 Selenium
  • 的子菜单元素引入 WebDriverwait
  • 为子菜单elementToBeClickable生成 WebDriverwait ,文本为 Selenium
  • 您可以使用以下解决方案:
  • 代码块:

  • 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:

    这篇关于org.openqa.selenium.interactions.MoveTargetOutOfBoundsException:(x,y)超出范围,而MouseHover和GeckoDriver Firefox Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆