Get Webelement id

629 views
Skip to first unread message

Peter Michalko

unread,
Feb 19, 2016, 5:05:46 AM2/19/16
to robotframework-users
   Q: Is it possible to get Webelemnt attribute "id"
  I want to check all html h2 for some attributes. I created a function Verify font style  to check element properties using javascript. But to use those functions I need "id" of those elements.
 Is there a way in Robot Framework how to get "id' of Webelement?

  @{elements} =    Get Webelements    xpath=//h2       #1. get all webelements h2
    : FOR    ${ELEMENT}    IN    @{elements}
    \    ${id}    Get Element Attribute    ${ELEMENT}@id   # this does not work
    \    Verify  font style    ${id}      ${col_subtext}    start    16px     300    # here I need id

Thank you for your suggestions.

Kevin O.

unread,
Feb 19, 2016, 10:07:10 AM2/19/16
to robotframework-users
Using Webelements in keykword Get Element Attribute is not yet supported. (issue).
For now you can call the get_attribute method on the elements. Here are two ways to do that:

    :FOR    ${ELEMENT}    IN    @{elements}
    \    ${id}=    Call Method    ${ELEMENT}    get_attribute    id
    \    ${id}=    Set Variable    ${ELEMENT.get_attribute('id')}

FYI I noticed that if an element does not have an id, then if you try to get it, the value is an empty string (in Chrome at least). This is not the behavior for other attributes, where value None is returned if they do not exist.
Reply all
Reply to author
Forward
0 new messages