Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

Python & Selenium - how do I find all element IDs on a page?

Writer Emily Wong

I know that I can use methods such as:

find_elements_by_tag_name()
find_elements_by_id()
find_elements_by_css_selector()
find_elements_by_xpath()

But what I would like to do is simply get a list of all the element IDs that exist in the page, perhaps along with the tag type they occur in.

How can I accomplish this?

1

2 Answers

from selenium import webdriver
driver = webdriver.Firefox()
driver.get(')
ids = driver.find_elements_by_xpath('//*[@id]')
for ii in ids: #print ii.tag_name print ii.get_attribute('id') # id name as string
0

Not had to do this before, but thinking about it logically you could use XPath to do this (may be other ways, XPath is the first thing that appears into my head).

Use find_elements_by_xpath using the XPath //*[@id] (any element that has an ID of some sort).

You could then iterate through the collection, and use the .tag_name property of each element to find out what kind of element it is and the get_attribute("id") method/function to get that element's ID.

Note: This is probably going to be quite slow. After all, you are asking for a lot of information.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy