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 find every element on page using attributes without values?

Example code “`
<span class="_7UhW9 xLCgt qyrsm KV-D4 se6yk T0kll ">cristiano</span>

How to tell python to find every element with *class*, *title*, *href*, and *tabindex* attributes. Then get the "title" value for each.

tried find_element_by_xpath but no result

>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

The syntax is "//*[@attribute_name]" i.e.
to get all the elements with title attribute the XPath is "//*[@title]",
to get all the elements with class attribute the XPath is "//*[@class]",
to get all the elements with href attribute the XPath is "//*[@href]"
etc.
So, to get all the title attribute values from all the elements on the page you can do something like this:

elements = driver.find_elements(By.XPATH, "//*[@title]")
for element in elements:
    title = element.get_attribute("title")
    print(title)

etc.
In case you want to locate all elements having title and class and href attributes, all the 3 in each all of them, it will look like this:

elements = driver.find_elements(By.XPATH, "//*[@title and @class and @href]")
for element in elements:
    title_val = element.get_attribute("title")
    class_val = element.get_attribute("class")
    href_val = element.get_attribute("href")
    print(title_val)
    print(class_val)
    print(href_val)
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