Extracting element IDs of required forms in selenium/python

Question:

I’m crawling this page (https://boards.greenhouse.io/reddit/jobs/4330383) using Selenium in Python and am looping through all of the required fields using:

required = driver.find_elements_by_css_selector("[aria-required=true]").

The problem is that I can’t view each element’s id. The command required[0].id (which is the same as driver.find_element_by_id("first_name").id returns a long string of alphanumeric characters and hyphens – even though the id is first_name in the HTML. Can someone explain to be why the id is being changed from first_name to this string? And how can I view the actual id that I’m expecting?

Additionally, what would be the simplest way to reference the associated label mentioned right before it in the HTML (i.e. "First Name " in this case)? The goal is to loop through the required list and be able to tell what each of these forms is actually asking the user for.

Thanks in advance! Any advice or alternatives are welcome, as well.

Asked By: John D'Uva

||

Answers:

driver.find_element_by_id("first_name") returns a web element object.
In order to get a web element attribute value like href or idget_attribute() method should be applied on the web element object.
So, you need to change your code to be

driver.find_element_by_id("first_name").get_attribute("id")

This will give you the id attribute value of that element

Answered By: Prophet

Your code is almost good. All you need to do is use the .get_attribute() method to get your id’s:

required = driver.find_elements_by_css_selector("[aria-required=true]")
for r in required:
    print(r.get_attribute("id"))
Answered By: Jonathan PETIT

I’m going to answer my second question ("How to reference the element’s associated label?") since I just figured it out using the find_element_by_xpath() method in conjunction with the .get_attribute("id") solutions mentioned in the previous answers:

ele_id = driver.find_element_by_id("first_name").get_attribute("id")
label_text = driver.find_element_by_xpath('//label[@for="{}"]'.format(ele_id)).text
Answered By: John D'Uva