Get Attribute function to return attribute or None

So the keyword documentation says the following:

" When a attribute is selected that is not present and no assertion operator is set, the keyword fails. If an assertion operator is set and the attribute is not present, the returned value is None . This can be used to assert check the presents or the absents of an attribute."

I have now tried to use the assertions in order to return the attribute and if one is not present to return None. However I have failed. Is it even possible?

The reason why I need this functionality is that in order to test some functionalities on the product I’m testing I need to fill in some mandatory fields. Some of these field need specific input and some general. The inputs are in different forms, text, numerical, checkbox’s, radio buttons, calendar entries etc. None of these elements have one attribute that is present in all, so I need to get multiple attributes that I use to auto-fill these information’s. On Selenium it loos like this:


${class}= Get Element Attribute ${i} class
${id}= Get Element Attribute ${i} id
${aria_label}= Get Element Attribute ${i} aria-label
${text}= Get Text ${i}

Run Keyword If “Action name” in “${placeholder}” Input Text ${i} ${action} ${number}
… ELSE IF “text” in “${type}” Input Text ${i} Some text inputed
Run Keyword If “number” in “${type}” Input Text ${i} 123
Run Keyword If “listbox” in “${role}” Click Element ${i}

So as you see, I use the attribute type to determine what the element is and how it should be handled and that is why I ask for a bunch of attributes, so I don’t need to assert whether or not any specific attribute is present, I just need the attribute or none.

Would Run Keyword And Ignore Error from the BuitIn Library help? you could use this with Get Element Attribute and first check if that passed if it did you can then check the attribute and perform your action.

I’m not sure which keyword documentation you were quoting there, as it’s not the current version of Get Element Attribute. I’m guessing you’re using SeleniumLibrary? is it an old version?

Hope this helps,

Dave.

I have actually tried “Run Keyword And Ignore Error” but the funny thing about Browser is that it ignores that keyword and still fails.

I was quoting the Browser keyword documentation, I’m trying to migrate our Selenium tests to Browser and the code I used was for Selenium and I gave it as an example of what I did with Selenium. Now I’m trying to find out how to do it in Browser.

Now I could use “Get Attribute Names” and then run “Get Attribute” if that attribute exists but something like this is not very sexy:

${list_of_attributes} Get Attribute Names ${element}
${type}= Set Variable None
${type} Run Keyword If “type” in ${list_of_attributes} Get Attribute ${element} type

Hi Ari,

I would not have guessed you were using Browser from your first message, just a tip, make these things clear in your initial post and you’ll get better or quicker answers.

You could create your own keyword for “Get Element Attribute” for your browser tests, it could look like something like this:

*** keywords ***
Get Element Attribute
    [Arguments]    ${element}    ${attr}
    ${list_of_attributes}    Get Attribute Names    ${element}
    ${value}=    Set Variable    None
    ${value}=    Run Keyword If    ${attr} in ${list_of_attributes}    Get Attribute    ${element} 
   ${attr}
    [Return]    ${value}

Would that work for you?

Dave.

1 Like

I’m sorry, I just thought that since this is the sub section dedicated for Browser it would not be necessary to specifically state that I use Browser. I’l be more clear in the future.

Anyways, that’s pretty much what I went with, I was just thinking there might be built-in function and I just missed it.

Hi Ari,

So you did, sorry I missed that as I picked it up from the latest list.

If it’s not in the doc I treat it as doesn’t exist, Browser is not a drop in replacement for selenium, some things they take quite a different approach, if something was useful to me in Selenium library and there is no equivalent I just create a keyword for it, but I guess you could also raise a feature request with the Browser team, my usage of Browser has been pretty light.

Good luck with your migration,

Dave.

Hi Dave

Well the problem with this one was that the documentation was a bit vague.
It says that with no assertion it fails, but with some assertion it returns, so I figured that there might be some way for it to work in a way that it returns the attribute if it exists or none if it does not exist. Since it clearly capable of doing both of them in some situations.

But I suppose I might have to do some keywords of my own, I don’t think this is such a pressing issue to request a feature.

Thanks

Ari

1 Like