问题
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