How to get a string from an element, regardless of element type

Hi folks,

I found out, that there are Get Text and Get Value keywords, to obtain the string from defined element. However, these keywords will work only if you know for sure, that the element in provided locator have string in Text or Value attribute.

I want to create a locator, which returns multiple elements, where some of them may have string stored in Text and some of them in Value. Is there a keyword, function or different trick, how to get the string from an element, regardless of its type?

I tried to use Evaluate with a piece of JavaScript I found somewhere, but it gives me some syntax error, which I’m unable to fix. I just don’'t see an obvious error? :slightly_smiling_face: But I guess it will not provide me with the result I want anyway.

Any idea? Thank you in advance.

Hi Pavel,

An idea, would be to build a keyword that check whether one or the other (text or value) exists, then depending of this get the other.
I see in your screen that the ${login.button} has no text but a value, so something like this :

[Arguments]    ${myelement}
${hasValue}    Run Keyword and Return Status  Get Element Attribute    ${myelement}    value
IF    ${hasValue} == ${True}
   ${content}    Get Element Attribute    ${myelement}    value
    ${content}    Get Text    ${myelement}
RETURN    ${content}

You could also a a check for length >0 maybe.


1 Like

Hi Charlie,

Thank you for the suggestion. It’s almost exactly what I thought about, as a “last resort” solution :slightly_smiling_face: I just hoped for something more straightforward and one-method/line solution. But it seems I will have to live with your solution. Thank you!