Reputation: 319
I am trying to get href
attribute from an html
list using Robot Framework keywords
. For example suppose the html
code
<ul class="my-list">
<li class="my-listitem"><a href="...">...</li>
...
<li class="my-listitem"><a href="...">...</li>
</ul>
I have tried to use the keywords WebElement
, WebElements
and for loop
without success. How can I do it?
This is my MWE
*** Test Cases ***
@{a tags} = Create List
@{href attr} = Create List
@{li items} = Get WebElements class:my-listitem
FOR ${li} IN @{li items}
${a tag} = Get WebElement tag:a
Append To List @{a tags} ${a tag}
END
FOR ${a tag} IN @{a tags}
${attr} = Get Element Attribute css:my-listitem href
Append To List @{href attr} ${attr}
END
Thanks in advance.
Upvotes: 0
Views: 3612
Reputation: 20067
The href
is an attribute of the a
elements, not the li
, thus you need to target them. Get a reference for all such elements, and then get their href
in the loop:
${the a-s}= Get WebElements xpath=//li[@class='my-listitem']/a # by targeting the correct element, the list is a reference to all such "a" elements
${all href}= Create List
FOR ${el} IN @{the a-s} # loop over each of them
${value}= Get Element Attribute ${el} href # get the individual href
Append To List ${all href} ${value} # and store it in a result list
END
Log To Console ${all href}
Upvotes: 1
Reputation: 3737
Here is a possible solution (not tested):
@{my_list}= Get WebElements xpath=//li[@class='my-listitem']
FOR ${element} IN @{my_list}
${attr}= Get Element Attribute ${element} href
Log ${attr} html=True
END
Upvotes: 0