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



Python


In this article, we show how to find the location 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 location of any HTML element on a web page.

To find the location 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 location using the location attribute.

This is shown in the code below.



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

Below we show the full code how to get the location 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 location by the line, python_image.location

The location attribute gives us the x and y coordinates of the image.

So the image is located 188px from the left side of the page (x coordinate) and 256px from the top of the page (y coordinate).

And this is how we can find the location, the x and y coordinates, 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...