how to scroll google sheets with Selenium webdriver

Viewed 29

I have a project I'm working on that writes to a google sheet using their API, but to be useful the user would like to see the google sheet and the cells the API writes to, so Im using selenium to open a window to the sheet and scroll down to keep up with the cell the API writes to

Ive come across code that looks like driver.execute_script("window.scrollBy(0,250);", "") but that doesnt seem to work on the google sheets page, no error, just doesnt do anything.

Ive also found the body element driver.find_element(By.XPATH, "//Body[@dir='ltr']") and the scroll bar element driver.find_elements(By.CLASS_NAME , "native-scrollbar") of the page but I cant find anything useful to do with these, though I feel there may be a workaround using one of these.

my ideal output would be the ability to scroll down by a set amount of pixels, or to a specified cell inside a google sheets page using a selenium chromewebdriver

ps. I have the Javascript tag because I have the option to run JS commands straight to the console using `driver.execute_script()'

1 Answers

I found the answer after a bit more testing and blind luck, its not as controllable as Id like but it does the job, let me know if you think you have a way to add more precision to this

the native scrollbars driver.find_elements(By.CLASS_NAME , "native-scrollbar") are actually the buttons at the end of the vertical and horizontal scrollbars, so this block allows you to scroll down but 13 cells (I think the 13 is just a product of how long the driver.click() holds for)

scrollbars = driver.find_elements(By.CLASS_NAME , "native-scrollbar")
scrollbars[0].click()
Related