如何在 watir 中滚动 Web 应用程序 [英] how to scroll a web application in watir

查看:55
本文介绍了如何在 watir 中滚动 Web 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Watir 中滚动 Web 应用程序?

How do I scroll a web application in Watir ?

我试过了@browser.send_keys :space

这只会让整个页面都下来.但是我在应用程序中有一个滚动条,我需要向下滚动垂直滚动条 &在我的自动化测试中,请帮助我!

This just brings the whole page down. But I have a scroll within the application, I need to scroll the vertical scroll bar down & up in my automation testing, Please help me !

谢谢!

<div dojoattachpoint="containerNode" class="containerNode tabContentPane typeNavigationSingleChild" style="overflow: auto; left: 5px; top: 10px; width: 1549px; height: 535px;">
  <div pageid="lifecycle_theme_home_page_dashboard_pageId" id="lifecycle_theme_home_page_dashboard_pageId" style="height: 535px; padding: 0px; width: 1549px;" widgetid="lifecycle_theme_home_page_dashboard_pageId" title="" role="group" class="dijitContentPane wcs-nullLayout">    

推荐答案

解决方案 1) 滚动到最后一个元素

我认为 Vinay 的方法应该有效.但是,在当前表单中,它假定该元素已存在于页面上.我猜你想要的元素只有在你滚动到足够远的地方才能看到.所以你可以做的是滚动到 div 中的最后一个元素.

I think Vinay's approach should work. However, in the current form, it assumes that the element already exists on the page. I am guessing the element you want is only visible once you scroll far enough. So what you can do is scroll to the last element in the div.

Watir-Webdriver

在 Watir-Webdriver 中:

In Watir-Webdriver:

div_with_scroll = browser.div(:class => 'containerNode tabContentPane typeNavigationSingleChild')
div_with_scroll.elements.last.wd.location_once_scrolled_into_view

Watir-经典

在 Watir-Classic 中,它是不同的,因为它不使用 selenium-webdriver:

In Watir-Classic, it is different since it does not use selenium-webdriver:

div_with_scroll = browser.div(:class => 'containerNode tabContentPane typeNavigationSingleChild')
div_with_scroll.elements.last.document.scrollIntoView

解决方案 2) 使用 ScrollTop 属性

作为替代,如果上述方法不起作用,您可以设置 scrollTop 属性来移动 div 元素的滚动条.这适用于我正在开发的应用程序,该应用程序的内容仅在您滚动到底部时才加载.

As an alternative, if the above does not work, you can set the scrollTop property to move the div element's scrollbar. This worked for an application that I was working on that had content that was only loaded once you scrolled to the bottom.

Watir-Webdriver

要将滚动条跳到底部,理论上应该触发下面的内容加载,请将scrollTop属性设置为scrollHeight:

To jump the scrollbar to the bottom, which in theory should trigger the below content to load, set the scrollTop property to the scrollHeight:

div_with_scroll = browser.div(:class => 'containerNode tabContentPane typeNavigationSingleChild')
scroll_bottom_script = 'arguments[0].scrollTop = arguments[0].scrollHeight'
div_with_scroll.browser.execute_script(scroll_bottom_script, div_with_scroll)

要跳回顶部,请将 scrollTop 设置为零.

To jump back to the top, set the scrollTop to zero.

div_with_scroll = browser.div(:class => 'containerNode tabContentPane typeNavigationSingleChild')
scroll_top_script = 'arguments[0].scrollTop = 0'
div_with_scroll.browser.execute_script(scroll_top_script, div_with_scroll)

您也可以根据需要使用的任何值在两者之间使用.

You can also use any value in between depending on where you need to go to.

Watir-经典

在 Watir-Classic 中,您可以更直接地设置 scrollHeight:

In Watir-Classic, you can set the scrollHeight more directly:

div_with_scroll = browser.div(:class => 'containerNode tabContentPane typeNavigationSingleChild')

#Go to bottom
div_with_scroll.document.scrollTop = div_with_scroll.document.scrollHeight

#Go to top
div_with_scroll.document.scrollTop = 0

这篇关于如何在 watir 中滚动 Web 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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