Scroll element in selenium VBA

二次信任 提交于 2021-01-29 06:41:48

问题


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.

I have tried too

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

But the same problem is still there.


回答1:


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


来源:https://stackoverflow.com/questions/65213243/scroll-element-in-selenium-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!