Get scroll height or height of an element and not body selenium

Viewed 1205

I want to find height of an element having class name rslwrp how to do this:

stores_container = driver.find_elements_by_class_name('rslwrp')

I know how to get body scrollheight

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

but how to find height of stores_container ?

Edit this is how i initialise driver:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

Am i missing some headers ?

2 Answers

driver.execute_script("return document.body.scrollHeight") will give you the scrollHeight of the body element. To get the scrollHeight of another element, replace body with querySelector("<element>"), in your case like so:

driver.execute_script("return document.querySelector('.rslwrp').scrollHeight") In this case, rslwrp would be a class.

Selecting the element with Selenium will not let us access its scrollHeight. In the other answer, he is accessing the size of the element which isn't a property of it in JavaScript like scrollHeight is.

For anyone not familiar with JS/CSS , querySelector takes in a CSS selector inside the quotes, here is a small guide on how to select by element tag, ID, or class using this method:

querySelector("div") selects the first div element which it finds

querySelector(".class") use a period to select the first element with this class

querySelector("#id") hashtag for ID.

In python I would do this,

stores_container = driver.find_elements_by_class_name('rslwrp')[0]

size = stores_container.size
w, h = size['width'], size['height']

print (w, h)
Related