org.openqa.selenium.interactions.MoveTargetOutOfBoundsException:使用 GeckoDriver Firefox Selenium 时 MouseHover 时 (x, y) 超出范围
org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (x, y) is out of bounds while MouseHover with GeckoDriver Firefox Selenium
我正在学习如何使用 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);
}
但这并没有帮助。
然后我读到,最好 "pipe" 那些 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
*硒Java v3.141.59
* GeckoDriver v0.24.0
请帮忙。
使用 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();
我在 geckodriver
和 Actions
class 中发现了同样的问题。尽管您可以使用以下代码
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();
代码在我这边运行良好。如果有任何问题,请告诉我。
注意: 将鼠标指针放在网页屏幕之外,否则它会覆盖当前焦点。
尝试使用它 -
action.moveToElement(menu).build().perform();
Thread.sleep(500);
moveToElement(selenium).build().perform();
Thread.sleep(500);
moveToElement(seleniumWebDriver).click().build().perform();
Web Application is that the HTML DOM 的主要问题达到 document.readyState
等于 complete
甚至在文本为 [= 的子菜单元素之前35=]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();
}
}
浏览器快照:
请尝试下面的代码(如果你在框架内,你需要出来使用下面的代码):
WebDriver driver=new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,400)");
我正在学习如何使用 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);
}
但这并没有帮助。
然后我读到,最好 "pipe" 那些 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 *硒Java v3.141.59 * GeckoDriver v0.24.0
请帮忙。
使用 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();
我在 geckodriver
和 Actions
class 中发现了同样的问题。尽管您可以使用以下代码
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();
代码在我这边运行良好。如果有任何问题,请告诉我。
注意: 将鼠标指针放在网页屏幕之外,否则它会覆盖当前焦点。
尝试使用它 -
action.moveToElement(menu).build().perform();
Thread.sleep(500);
moveToElement(selenium).build().perform();
Thread.sleep(500);
moveToElement(seleniumWebDriver).click().build().perform();
Web Application is that the HTML DOM 的主要问题达到 document.readyState
等于 complete
甚至在文本为 [= 的子菜单元素之前35=]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(); } }
浏览器快照:
请尝试下面的代码(如果你在框架内,你需要出来使用下面的代码):
WebDriver driver=new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,400)");