【问题标题】:Mouse scroll simulation in selenium - pythonselenium 中的鼠标滚动模拟 - python
【发布时间】:2018-07-27 15:05:42
【问题描述】:

在废弃网站时,我必须滚动一个由许多 div 组成的表格。 我用下面的代码让滚动条移动到最后,

source_element = driver.find_element_by_xpath("//div[4]/div/div[103]/div[2]/div[1]/div[2]") ### Scrollbar element

dest_element = driver.find_element_by_xpath("//div[4]/div/div[103]/div[2]/div[1]/div[4]/span") ### ▼ element
ActionChains(driver).drag_and_drop(source_element, dest_element).perform()

但问题发生得非常快,并且表中的数据无法正确加载。我试图用 selenium - python 函数模拟鼠标滚动,但无法实现。除了使用 pynput(它可以模拟鼠标功能,但我必须将光标物理放置在网页上)之外,是否有任何解决方案来减慢它或任何其他方式

向下滚动时,它会加载更多表格条目。我也尝试使用偏移量拖放,但滚动条不移动,它抛出 MoveTargetOutOfBoundsException

【问题讨论】:

  • 您要向下滚动页面以触发 XHR 并加载更多表条目吗?
  • 对不起,我对 XHR 不熟悉,你能解释一下吗?也只有鼠标滚动或单击 n 拖动滚动条有效

标签: python selenium-webdriver selenium-firefoxdriver


【解决方案1】:

如果您知道元素的位置,那么您可以使用 javascript 执行器滚动到该元素。我不确定是否有其他缓慢滚动的解决方案,但如果您的要求是缓慢滚动以加载所有数据,那么您可以尝试滚动到任何元素,等待加载数据然后滚动到其他元素并重复。

JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement scrollto = driver.findElement(By.xpath("//div[4]/div/div[103]/div[2]/div[1]/div[4]/span")); //Find element by xpath and store in variable "Element"     
js.executeScript("arguments[0].scrollIntoView();", scrollto); //This will scroll the page till the element is found

【讨论】:

  • 对不起,我没有更新你。当我运行代码时,它没有抛出任何错误,并且在 UI 中滚动功能没有执行
猜你喜欢
  • 1970-01-01
  • 2018-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多