硒VBA中的滚动元素 [英] Scroll element in selenium VBA

查看:63
本文介绍了硒VBA中的滚动元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试着眼于某个元素,以使其在屏幕中央或从顶部到中间的中间位置.这就是我所做的

I am trying to focus on an element to make this element in the center of the screen or way from the top to be above the middle. This is what I did

.FindElementById("toBePaid[" & r - 1 & "]").ExecuteScript "this.scrollIntoView(true); window.scrollBy(0, -(window.innerHeight - this.clientHeight)-150);"

但是我看不到该元素,因为它在网站屏幕的最上方.

But I couldn't see the element as it is at the top-most of the screen of the website.

我也尝试过

.FindElementById("toBePaid[" & r - 1 & "]").ScrollIntoView True

但是同样的问题仍然存在.

But the same problem is still there.

推荐答案

借助@QHarr,我可以将它变得柔和

With the help of @QHarr I could dolve it

Function ScrollIntoViewCenter(element As WebElement)
  Const JS_SCROLL_CENTER = _
    "this.scrollIntoView(true);" & _
    "var y = (window.innerHeight - this.offsetHeight) / 2;" & _
    "if (y < 1) return;" & _
    "for (var e=this; e; e=e.parentElement) {" & _
    "  if (e.scrollTop == 0) continue;" & _
    "  var yy = Math.min(e.scrollTop, y);" & _
    "  e.scrollTop -= yy;" & _
    "  if ((y -= yy) < 1) return;" & _
    "}" & _
    "window.scrollBy(0, -y);"
  
  element.ExecuteScript JS_SCROLL_CENTER
End Function

这篇关于硒VBA中的滚动元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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