How to Find the Size of an Element on a Web Page using the Selenium Module in Python



Python


In this article, we show how to find the size, the height and width, of an element on a web page using the Selenium module in Python.

By element, it's meant an HTML element.

This may be an image, it may be a paragraph, it may be a text box, it may be a text area, etc.

The selenium module in Python is a very dynamic and useful module when working with web browsers such as Firefox or Chrome.

Using the selenium module, we can open up a browser, go to any URL we need to go to, and do things such as enter text into text boxes, submit forms, and find the size of any HTML element on a web page.

To find the size of an HTML element on a web page using the selenium module in Python, we target that element using identifiers such as the id attribute, name, tag name, CSS selector, etc to locate it. We then use obtain the size using the size attribute.

This is shown in the code below.



Once we have appropriately identified the HTML element, we can get its size through the size attribute.

Below we show the full code how to get the sizze of the Python logo image on the Python page of this website: http://www.learningaboutelectronics.com/Python/

If you view the source code of the page, you will see that the Python image has an id attribute of 'Python'.



So we open the Firefox web browser and go to the Python home page of this website.

We target the Python logo image by its id attribute, which is "Python"

We store this in the variable, python_image

We then find its size by the line, python_image.size

The size attribute gives us the height and width attributes.

So the image has a height of 62px and a width of 208px.

And this is how we can find the size, the height and width, of an HTML element on a web page using the Selenium module in Python.


Related Resources

How to Create a Zip File in Python

How to Extract All Files and Folders from a Zip File in Python

How to Read the Contents of a Zip File in Python



HTML Comment Box is loading comments...