【问题标题】:How to scroll custom scrollbar using Selenium WebDriver如何使用 Selenium WebDriver 滚动自定义滚动条
【发布时间】:2018-01-31 11:24:19
【问题描述】:

我不是以英语为母语的人,对不起我的英语。 :)

我正在使用 Selenium Webdriver + Python。我的页面上有一个自定义滚动条。

<div class="k-scrollbar k-scrollbar-vertical" style="width: 18px;"><div style="width:1px;height:1102px"></div></div>


    <div style="width:1px;height:1102px"></div>

这个滚动条的EV有方法“滚动”,代码是:

function updateGridScrollInState() {
  if (typeof window.GridProfile_Grid === 'object') {
    window.GridProfile.ScrollY = window.GridProfile_Grid.data("kendoGrid").virtualScrollable.verticalScrollbar.scrollTop();
  }
}

我可以使用它进行操作,我知道滚动条的定位器,但我不明白如何通过 driver.execute_script(some_script) 使用 javascript 将特定元素滚动到网格中。

我需要移动到位于视口之外的元素:它是网格,网格有 33 行,但只显示 22 行,如果我向下滚动则显示其他 11 行。我试图拖放这个滚动条:

ActionChains(driver).drag_and_drop(my_custom_scrollbar,  list_of_grid_rows[30]).perform()

但我得到了例外:

selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (1015, 1242.699951171875) is out of bounds of viewport width (1920) and height (968)

我想我可以在 list_of_grid_rows[index] 不显示的情况下进行拖放。但是我怎样才能得到这个结果呢?也许我必须以某种方式使用滚动条的高度值?

谢谢你的提前!

【问题讨论】:

    标签: javascript python selenium


    【解决方案1】:

    python API 有一个元素属性,它可以使所需元素滚动到视图中。我认为即使页面上有自定义滚动,它仍然可以工作。第一步是获取要滚动到视图中的元素的定位器。听起来你有那个。我们称之为[元素]。接下来,如果您已验证它存在于页面上,则执行以下操作:

    coordinates = [element].location_once_scrolled_into_view
    

    即使您可能不关心坐标,此命令也会使目标滚动到视图中。 Selenium Python Element API.

    【讨论】:

    • 它正在工作!谢谢!当我在等待答案时,我也找到了解决方案:driver.switch_to.default_content() #switch to griddriver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_xpath(xpath).find_elements_by_tag_name('tr')[index]) #after switch_to() I can use scrollIntoView() from JavaScript, it will scroll down my grid alreadydriver.find_element_by_xpath(xpath).find_elements_by_tag_name('tr')[index].click() 但你的答案更有用。再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    相关资源
    最近更新 更多