【问题标题】:org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (x, y) is out of bounds while MouseHover with GeckoDriver Firefox Seleniumorg.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (x, y) is out of bounds while MouseHover with GeckoDriver Firefox Selenium
【发布时间】:2019-08-20 03:47:40
【问题描述】:

我正在学习如何使用 Selenium WebDriver 自动化测试,但是我卡住了,无法让下拉菜单在 Firefox 中工作。相同的代码在 Chrome 中运行良好。

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

错误信息表明网页元素可能还没有加载到屏幕上,所以我实现了一些方法来等待每次执行,直到元素出现:

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

但它没有帮助。

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

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

到一行。此时它开始在 Chrome 上运行,但我仍在努力让它在 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 我收到错误消息:

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

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

请帮忙。

【问题讨论】:

  • 首先要检查的是浏览器的缩放级别,它可能会导致悬停功能失效。它应该始终是 100%。如果您的缩放级别很好,您可能会遇到这种情况:github.com/mozilla/geckodriver/issues/1507
  • @Ardesco 缩放级别很好。另外,我认为我不受链接问题的影响。一些建议的答案有效,但只是部分有效。我仍在寻找完美的解决方案。

标签: java selenium firefox geckodriver mousehover


【解决方案1】:

Web Application 的主要问题是 HTML DOM 达到 document.readyState 等于 complete 甚至在文本为 Selenium WebDriver 的子菜单元素之前strong> 被渲染。因此,您将错误视为:

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

解决方案

所以理想的解决方案是:

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

        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();
            }
        }
    
  • 浏览器快照:

【讨论】:

  • 与@kajal-kundu 的代码情况相同。鉴于鼠标指针不在网络浏览器中,它适用于 Firefox,但它不再适用于 Chrome。它打开菜单“自动化工具”,但转到子菜单“BDD”。
  • @kajal-kundu 的代码是什么意思? 鼠标指针不在网络浏览器中是什么意思?您特别提到 当我切换到 Chrome 时同样可以正常工作,但使用 Firefox 时出现错误。此答案专门针对 Firefox 。你能复制粘贴代码并执行并更新我的状态吗???
  • Kajal Kundu 也回答了我的问题。如果鼠标光标位于 Web 浏览器窗口上,则提供的解决方案将不起作用。我已经执行了你的代码,它可以在 Firefox 中运行(假设光标不在网络浏览器上),但它不再适用于 Chrome,因此我无法接受答案。如果没有必要,我不想为不同的网络浏览器创建多个相同的方法。
  • 我不认为 Kajal Kundu 与这个答案有关。我不知道您所说的 鼠标光标位于 Web 浏览器窗口上 到底是什么意思,因为我们大多数开发人员都会从 IDE 触发执行,例如蚀。重申一下,您特别提到 当我切换到 Chrome 时同样可以正常工作,但使用 Firefox 时出现错误。虽然这个答案是通用的,但特别适用于 Firefox。我错过了什么吗?
  • 奇怪的是,重启后它开始在两个 Web 浏览器上正常使用您的代码。谢谢你:)
【解决方案2】:

请尝试下面的代码(如果你在框架内,你需要出来使用下面的代码):

WebDriver driver=new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,400)");

【讨论】:

    【解决方案3】:

    使用WebDriverWait 并尝试以下代码。

    driver.get("http://executeautomation.com/demosite/index.html");
    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement menu= wait.until(ExpectedConditions.elementToBeClickable(By.id("Automation Tools")));
    
    Actions action = new Actions(driver);
    action.moveToElement(menu).build().perform();
    WebElement selenium =wait.until(ExpectedConditions.elementToBeClickable(By.id("Selenium"))); 
    action.moveToElement(selenium).build().perform();
    WebElement seleniumWebDriver =wait.until(ExpectedConditions.elementToBeClickable(By.id("Selenium WebDriver")));
    action.moveToElement(seleniumWebDriver).click().build().perform();
    

    【讨论】:

    • 它适用于 Firefox,因为鼠标指针不在网络浏览器中,但它不再适用于 Chrome。它打开菜单“自动化工具”并转至子菜单“BDD”。我也尝试使用 By.xpath 搜索元素,但没有成功。
    • 我的输出仅来自 Chrome,而您说它不适用于 Chrome。抱歉,我无法帮助您。
    【解决方案4】:

    尝试使用它 -

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

    【讨论】:

    • 试过了,但同样的问题仍然存在。此外,您的代码缺少“操作”。在方法 moveToElement 之前。
    【解决方案5】:

    我在 geckodriverActions 类中观察到了同样的问题。虽然您可以使用以下代码

    System.setProperty("webdriver.gecko.driver", "C:\\Drivers\\geckodriver-v0.24.0-win64\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.get("http://executeautomation.com/demosite/index.html");
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    WebElement mainmenu = driver.findElement(By.xpath("//li[@class='active has-sub']"));
    WebElement submenu = driver.findElement(By.xpath("//li[@class='has-sub'] [contains(.,'Selenium')]"));
    WebElement intendedLink = driver.findElement(By.xpath("//li[@class='has-sub'] [contains(.,'Selenium')]//li[contains(.,'Selenium WebDriver')]"));
    
    Actions action =new Actions(driver);
    action.moveToElement(mainmenu).clickAndHold().build().perform();
    Thread.sleep(1000);
    action.moveToElement(submenu).clickAndHold().build().perform();
    Thread.sleep(1000);
    intendedLink.click();
    

    我的代码运行良好。如果有任何问题,请告诉我。

    注意:不要让鼠标指针离开网页屏幕,否则它会覆盖当前焦点。

    【讨论】:

    • 它适用于 Firefox,因为鼠标指针不在网络浏览器中,但它不再适用于 Chrome。它会打开菜单和子菜单,但不要单击“Selenium WebDriver”Web 元素。另外,我不明白您为什么使用 clickAndHold() 方法而不是 click()。有什么区别?我想它会“按住”LMB,移动光标并释放LMB。如果我尝试在浏览器中手动执行此操作,则不会出现下拉菜单,因此我很困惑。
    猜你喜欢
    • 2020-05-22
    • 2021-05-07
    • 2017-04-09
    • 2021-10-18
    • 2021-12-17
    • 1970-01-01
    • 2017-05-20
    • 2016-09-14
    • 1970-01-01
    相关资源
    最近更新 更多