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.
CodePudding user response:
driver.find_element_by_id("first_name")
returns a web element object.
In order to get a web element attribute value like href
or id
- get_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
CodePudding user response:
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"))