我正在学习如何使用 Selenium WebDriver 进行自动化测试,但是我卡住了,无法使下拉菜单在 Firefox 中工作。相同的代码在 Chrome 中运行得很好。
我练习的网站是: http://www.executeautomation.com/demosite/index.html 我想从菜单中单击以下项目:Automation Tools > Selenium > Selenium WebDriver。
错误消息表明 web 元素可能还没有加载到屏幕上,所以我实现了一些方法来等待每次执行直到元素出现:
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
请帮忙。
最佳答案
Web Application 的主要问题是HTML DOM达到 document.readyState
等于 complete
甚至在带有 Selenium WebDriver 文本的子菜单元素被呈现之前。因此,您看到的错误是:
Exception in thread "main" org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (-4899, 91) is out of bounds of viewport width (1366) and height (664)
解决方案
所以一个理想的解决方案是:
- 为
titleIs()
Execute Automation
引入 WebDriverwait
- 为带有文本的菜单元素引入 WebDriverwait 作为 Automation Tools
- 为带有 Selenium 文本的子菜单元素引入 WebDriverwait
- 为
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(); } }
浏览器快照:
关于java - org.openqa.selenium.interactions.MoveTargetOutOfBoundsException : (x, y) 使用 GeckoDriver Firefox Selenium 时 MouseHover 超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55413422/