Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if an html element has an attribute selenium python

I am using the get_attribute() function in python with selenium to get a specific attribute from the children of a specific html element, however, some of the children do not have that attribute, is there a way to check if an html element has a specific attribute?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You should iterate through direct children of that specific html element and check the desired attribute on each of them individually.

To get all the direct children one way is to use CSS selectors:

find_elements_by_css_selector("your_element > *")

The above gives you the list of childrens, then iterate over it and check if each element has the attribute you want using get_attribute().

If you already have the element, you can use XPATH as well:

e.find_elements_by_XPATH('./*')

note: find_elements_by_* commands are now deprecated. It’s better to use find_elements(By.XPATH) for example.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading