我的母语不是英语,我对我的英语感到抱歉。 :)
我正在使用 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有方法“Scroll”,代码是:
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 将特定元素滚动到网格中。
我需要移动到位于视口(viewport)之外的元素:它是网格,网格有 33 行,但只显示 22 行,如果向下滚动,则会显示其他 11 行。我尝试拖放此滚动条:
ActionChains(driver).drag_and_drop(my_custom_scrollbar, list_of_grid_rows[30]).perform()
但我得到了异常(exception):
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (1015, 1242.699951171875) is out of bounds of viewport width (1920) and height (968)
我想我可以在不显示 list_of_grid_rows[index] 的情况下拖放。但我怎样才能得到这个结果呢?也许我必须以某种方式使用滚动条的高度值?
谢谢您的提前!
最佳答案
Python API 有一个 element 属性,可以使所需的元素滚动到 View 中。我认为即使页面上有自定义滚动它仍然可以工作。第一步是获取要滚动到 View 中的元素的定位器。听起来你有这个。我们称之为[元素]。接下来,如果您已验证它存在于页面上,则执行以下操作:
coordinates = [element].location_once_scrolled_into_view
即使您可能不关心坐标,此命令也会导致目标滚动到 View 中。 Selenium Python Element API .
关于javascript - 如何使用 Selenium WebDriver 滚动自定义滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48540994/