florentbr/SeleniumBasic

Chrome cannot find element if not visible on monitor. Must scroll to display element in browser

Opened this issue · 1 comments

I use Excel VBA and Chrome to fill out textboxes on webpages that requires a lot of scrolling (lots of content on same webpage). Chrome requires the textbox element to be displayed on the browser or otherwise gives "element not found" error. All the elements are on that page where only certain elements are visible due to physical monitor size. I must use findelementbyID.scrollintoview and some extra javascript code to scroll a bit more to make the element visible in the browser. Then the element is found and there is no error.
Internet Explorer does not need the element to be visible on the screen monitor as long as the element HTML is on the page. How do I bypass the requirement to make element visible on Chrome because the scrolling is not accurate depending on the type of monitor resolution? I am assuming Microsoft Edge has same problem since it is based on Chrome.

Gby3 commented

Hi gitcano, you can use javascript to center the element in the middle of the viewport instead of scrolling "a bit more" (this will be done independently of your monitor size/resolution):

' Declare your textbox
Set Element = driver.findelementbyid(id)
Element.ExecuteScript _
"this.scrollIntoView(true);" & _
"window.scrollBy(0, -(window.innerHeight - this.clientHeight) / 2);"

Hope it helps!