有没有办法在硒1。X或2。x来滚动浏览器窗口,以便XPath标识的特定元素在浏览器的视图中?在Selenium中有一个focus方法,但在FireFox中它似乎不能物理地滚动视图。有人有什么建议吗?
我需要这个的原因是我正在测试点击页面上的一个元素。不幸的是,除非元素是可见的,否则事件似乎无法工作。我无法控制单击元素时触发的代码,因此无法调试或修改它,因此,最简单的解决方案是将项目滚动到视图中。
有没有办法在硒1。X或2。x来滚动浏览器窗口,以便XPath标识的特定元素在浏览器的视图中?在Selenium中有一个focus方法,但在FireFox中它似乎不能物理地滚动视图。有人有什么建议吗?
我需要这个的原因是我正在测试点击页面上的一个元素。不幸的是,除非元素是可见的,否则事件似乎无法工作。我无法控制单击元素时触发的代码,因此无法调试或修改它,因此,最简单的解决方案是将项目滚动到视图中。
当前回答
Selenium的默认行为是滚动,因此元素几乎不在视口顶部的视图中。此外,并非所有浏览器都具有完全相同的行为。这很不令人满意。如果您像我一样记录浏览器测试的视频,那么您需要的是元素滚动到视图中并垂直居中。
下面是我的Java解决方案:
public List<String> getBoundedRectangleOfElement(WebElement we)
{
JavascriptExecutor je = (JavascriptExecutor) driver;
List<String> bounds = (ArrayList<String>) je.executeScript(
"var rect = arguments[0].getBoundingClientRect();" +
"return [ '' + parseInt(rect.left), '' + parseInt(rect.top), '' + parseInt(rect.width), '' + parseInt(rect.height) ]", we);
System.out.println("top: " + bounds.get(1));
return bounds;
}
然后,为了滚动,你像这样调用它:
public void scrollToElementAndCenterVertically(WebElement we)
{
List<String> bounds = getBoundedRectangleOfElement(we);
Long totalInnerPageHeight = getViewPortHeight(driver);
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("window.scrollTo(0, " + (Integer.parseInt(bounds.get(1)) - (totalInnerPageHeight/2)) + ");");
je.executeScript("arguments[0].style.outline = \"thick solid #0000FF\";", we);
}
其他回答
我已经尝试了很多关于滚动的东西,但下面的代码提供了更好的结果。
这将滚动直到元素在视图中:
WebElement element = driver.findElement(By.id("id_of_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(500);
//do anything you want with the element
下面是我如何用PHP webDriver for Selenium做到这一点。它适用于Selenium独立服务器2.39.0 + https://github.com/Element-34/php-webdriver + Firefox 25.0
$element=$session->welement("xpath", "//input[@value='my val']");
$element->click();
$element=$session->welement("xpath", "//input[@value='ma val2']");
$element->location_in_view(); // < -- this is the candy
$element->click();
注意:我使用了Element34 php webdriver的定制版本。但是核心没有任何变化。我只是用了“welement”而不是“element”。但这对本案没有任何影响。驱动程序作者说:“允许几乎所有的API调用都是WebDriver协议本身定义的直接转换。”所以你使用其他编程语言应该没有问题。
在我的设置中,只是单击将不起作用。它将做滚动而不是点击,所以我不得不点击两次而没有调用“location_in_view()”。
注意:此方法适用于可以查看的元素,例如input类型为button。
来看看: http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element/:id/location
jsonwireprotocol#的描述建议使用location + moveto,因为location _in_view是一个内部方法。
在Java中,我们可以使用JavaScript进行滚动,如下所示:
driver.getEval("var elm = window.document.getElementById('scrollDiv'); if (elm.scrollHeight > elm.clientHeight){elm.scrollTop = elm.scrollHeight;}");
您可以为“elm”分配一个所需的值。scrollTop”变量。
你可以使用下面的代码片段来滚动:
C#
var element = Driver.FindElement(By.Id("element-id"));
Actions actions = new Actions(Driver);
actions.MoveToElement(element).Perform();
好了
我使用这种方式滚动元素并单击:
List<WebElement> image = state.getDriver().findElements(By.xpath("//*[contains(@src,'image/plus_btn.jpg')]"));
for (WebElement clickimg : image)
{
((JavascriptExecutor) state.getDriver()).executeScript("arguments[0].scrollIntoView(false);", clickimg);
clickimg.click();
}