- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在学习如何使用 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
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/
我在 Firefox Webdriver 上的 move_to_element 函数有问题(Chrome 运行良好) ${SCREEN_WIDTH} 1440 ${SCREEN_HEIGHT}
我在 Firefox Webdriver 上的 move_to_element 函数有问题(Chrome 运行良好) ${SCREEN_WIDTH} 1440 ${SCREEN_HEIGHT}
我在使用 selenium 的 FireFox/IE9 驱动程序时遇到了问题。当我使用 selenium 的 Actions 类并使用它的方法 moveToElement 时,它不会移动到元素并且会返
这个问题已经有答案了: Selenium - MoveTargetOutOfBoundsException with Firefox (9 个回答) Firefox - org.openqa.sele
我在 Firefox Webdriver(Chrome、IE 运行良好)上的函数 move_to_element 有问题 driver = webdriver.Firefox() driver.get
我不知道为什么 ActionChains move_to_element() 无法与 chromedriver >74 配合使用。 (但它适用于 chromedriver 74 和 geckodriv
我遇到了一个奇怪的情况,在 Serenity 的页面上我必须滚动到元素: withAction().moveToElement(webElement).perform(); 某些元素的这个方法抛出:
我正在学习如何使用 Selenium WebDriver 进行自动化测试,但是我卡住了,无法使下拉菜单在 Firefox 中工作。相同的代码在 Chrome 中运行得很好。 我练习的网站是: http
我是一名优秀的程序员,十分优秀!