如何移动到浏览器窗口中看不到的geb页面内容? [英] How to move to geb page content that is not visible in the browser window?

查看:113
本文介绍了如何移动到浏览器窗口中看不到的geb页面内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使geb Webdriver移至页面上可能不在浏览器视图中的元素.该元素存在于页面上并显示出来,但是有可能该元素会出现在页面底部,并且浏览器可能需要向下滚动才能将该元素显示出来.

How can I get the geb webdriver to move to an element on the page that may not be in the view of the browser. The element exists on the page and is displayed but there may be a possibility that the element will appear on the bottom of the page and the browser would need to scroll down in order to bring the element into view.

import geb.Page
class myPage extends Page {
  static url = "mypage.xhtml"
  static at = {title == "myPage"}
  static content = {
    someElement = {$("#bottomOfPage")}
    anotherElement = {$(".someClass",5)}
  }

  void clickElement(){
    //possibility to fail because element may not be displayed in browsers view
    //if not in view, scroll to element, then click
    someElement.click()
  }
}

以该页面为例,如果某个元素不在页面上并且可能不在最底部,则如何告诉Webdriver将元素显示在视图中?当页面内容不在视图中时,我有一些复杂的测试失败了.在某些情况下,即使不在视野中,驱动程序也会移至该元素.但是,我想明确地告诉驱动程序,如果它不自行移动,则不考虑将其移动到某个元素.

Using this page as an example, If an element is not in view on the page and possibly at the very bottom, how can I tell the webdriver to bring the element into view? I have some complicated tests that are failing when the page contents are not in view. In some cases, the driver will move to the element even if it's not in view. But I would like to explicitly tell the driver to move to an element if its not in view for the cases that it does not move on its own.

硒库确实具有moveToElement()函数,但它仅接受WebElement类的对象.使用geb,在创建页面类时,在我的测试中使用的所有元素都在内容部分中声明,因此属于SimplePageContent类.同样,SimplePageContent也不能强制转换为WebElement. moveToElement(By.id("bottomOfPage"))确实可以工作,但是并不理想,因为我不需要硬编码的值.如果可能的话,我更愿意传递页面内容对象本身.

The selenium library does have a moveToElement() function but it only accepts objects of the WebElement class. Using geb, when creating page classes, all elements that are used in my tests are declared in the content section and thus are of the SimplePageContent class. SimplePageContent cannot be casted to a WebElement either. moveToElement(By.id("bottomOfPage")) does work but is not ideal because I do not want hard coded values. Id much rather pass the page content object itself if possible.

推荐答案

根据我的经验,我可以说,如果让WebDriver单击当前不在视图中的元素,它将首先滚动到该元素,然后单击它.因此,您要么遇到某种错误(如果没有发生),要么该元素不在视野中,这不是导致您的元素未被点击的原因.

From my experience I can say that if you ask WebDriver to click on an element that is not currently in view it will first scroll to that element and then click on it. So you're either hitting some kind of bug if it does not happen or the fact that the element is not in view is not what's causing your element not to be clicked.

如果您仍然想使用Actions.moveToElement()显式移动到元素,则可以轻松实现内容定义返回的对象,因为它们实现了Navigator并附带了 ,当您的Navigator恰好有一个WebElement时.

If you still want to move to the element explicitly using Actions.moveToElement() then you can easily turn objects returned from your content definition using the fact that they implement Navigator which comes with a firstElement() which you should use if you want to get the first WebElement of a multi element Navigator or singleElement() when your Navigator has exactly one WebElement.

还有一种更简单的方法,不需要从您的Navigator中提取WebElement-使用

There is also a simpler way, which won't require extracting WebElements from your Navigators - use an interact {} block:

interact {
    moveToElement(someElement)
}

这篇关于如何移动到浏览器窗口中看不到的geb页面内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆