2

If I have the following HTML <a id="id" class="class" href="href">Element Text</a> how would I get "id" to be returned? My current procedure is:

print("Attribute: " + element.get_attribute('id')) print("Property: " + element.get_property('id')) print("Class: " + element.get_attribute('class')) 

But all of those return empty strings. I am, however, able to get the text by using element.text

EDIT: Here's a more in depth explanation

I'm looking for an element but that element's ID varies. There is, however, an element linked to the element I want that I can find using it's xpath and comparing it's text to a specific text that I know beforehand. The ID of that element is something in the form of someID_XX. By taking the XX and appending it to another fixed string, I can then search for the element that I actually want. My issue is that once I get the second element (not the one I want directly, but the one that can lead me to the one I want) I can't seem to get it's ID attribute even though it seems to have one in the html. My question is, how do I get the id attribute?

6
  • Did you try :driver.find_element_by_id('id')CommentedOct 23, 2017 at 14:02
  • I have the element already, found using xpath. Now I need the id of the element.CommentedOct 23, 2017 at 14:06
  • What selector did you use to locate element?
    – Andersson
    CommentedOct 23, 2017 at 14:20
  • xpath: //*[@id="id"]. The issue is that the actual id is "id_XX" where XX is a varying number. I find this element by searching for a string and then I need the XX to find another element which uses XX in it's IDCommentedOct 23, 2017 at 14:49
  • 1
    the "id" was just a placeholder to make the question simpler to understand. There is a specific element that I want but it has a varying ID so I can't search for it directly. The only way I can search for it is by looking for another element that has very specific text. Once I find that element (I find it using that element's xpath) I was hoping to get it's ID using get_attribute("id") which I could then use to find the element that I actually want. I've update the OP to explain this process a bit better.CommentedOct 23, 2017 at 16:27

1 Answer 1

1

For me is working with

element.get_attribute('id') 

At the beginning didn't work because I selected incorrectly a hidden javascript element without id. But if the error persist on your case, you can try this:

element.get_attribute('outerHTML') 

get the DOM for the element, and then parse the html to extract the id

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.