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

查看:12
本文介绍了如何移动到浏览器窗口中不可见的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.

selenium 库确实有一个 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 的事实轻松地转换从内容定义返回的对象> 带有 firstElement() 如果您想获得多元素 NavigatorsingleElement() 当您的 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 - 使用 interact {} 块:

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天全站免登陆