Get Attribute as null while there is value?


#1

Hi. Just wondering why does the “Get Attribute” method returns: Attribute ‘text’ of object ‘Object Repository/Page_Works Order Master File/div_janice’ is: ‘null’

02-18-2019 04:10:56 PM getAttribute(findTestObject(“Page_Works Order Master File/div_janice”), “text”)

Elapsed time: 4.055s

Attribute ‘text’ of object ‘Object Repository/Page_Works Order Master File/div_janice’ is: ‘null’

when the element itself has text as its attribute?

Would like to ask for help from anyone who might know what happened there.

Thank you.


#2

Hello,
can you also post snippet of HTML you are trying to adress?


#3

Hi, thanks for the prompt reply. You mean this?

<div class="k-spreadsheet-cell" style="color: rgb(48, 48, 48); font-family: Arial; font-size: 12px; white-space: pre; overflow-wrap: normal; left: 201px; top: 1px; width: 199px; height: 19px;"><div class="k-vertical-align-bottom">janice</div></div>


#4

yes, that was what i wanted…
i think you are trying to get text in div by using getAttribute?
something like:
WebUI.getAttribute(TO, 'text')
is actually looking for attribute named text and will return ‘myText’ in following case:
<div text='myText'>Content Text</div>
to get ‘Content Text’ use
WebUI.getText(TO)


#5

Some other approaches are:

WebUI.getAttribute(findTestObject(“Page_Works Order Master File/div_janice”), "textContent");

or

WebUI.getAttribute(findTestObject(“Page_Works Order Master File/div_janice”), "innerText");

Either of these will do what you’re trying to do here.


#6

Neither of which are attributes :roll_eyes:

#JustSayin


#7

Indeed. Although Se doesn’t make any distinction between “attribute” and “property” either, so it’s really not a fault of the Katalon API…


#8

Sorry. I am not from a too technical background. Do you mind to explain the the difference between attribute and property you and Brandon_Hein were talking about?? I thought the text retrieved in the image I attached above is one of the HTML attribute? Is it not?


#9

I see. Thank you. I did not realize the difference between text and content text. I manage to verify it using “Verify Element Text”. I suppose this keyword is to verify the content text then. While "Verify Element Attribute Value: text: is to verify the myText in <div text= /myText' >Content Text</div>


#10

just one remark, you dont need to check for text, since in definition of TO is text as a part of final XPATH. if you expect also other text then janice, uncheck “text equals janice” in TO definition.


#11

This post sums up the difference between attribute and property fairly well:

The idea is that when a browser parses HTML code, it creates objects to represent each node that it finds in the HTML. These objects have a bunch of properties that define the state of the node that they represent. You can easily see this using your browser console. Here’s an example for the Google search field element:

And this list goes on for a while. These are all properties. In my solution from above, “textContent” and “innerText” are both properties in this list as well.

Now for attributes. Attributes are the key-value pairs that you see in element nodes. Here’s the element that represents the same search field as my example above:

All of the extra data you see after the “input” tag (class, name, type, etc.) are considered attributes.

When Russ says:

He’s pointing out that the WebUI.getAttribute() method is a misnomer, because it can be used to get BOTH attributes and properties. For instance, I can do something like:

WebUI.getAttribute(testObject, "class");

which gets the class attribute value, but I can also use it to get the textContent property value as well:

WebUI.getAttribute(testObject, "textContent");

However, the WebUI methods are actually based in part on another library called Selenium, which also does not make a distinction between getting property values and attribute values:

WebElement element;
element.getAttribute("class");
element.getAttribute("textContent");

As to your question about “getting the text from an element”, it works a bit differently. Lets take the HTML snippet you gave above:

<div class="k-vertical-align-bottom">janice<div>

When you see text in the HTML (‘janice’ from the above code), it’s not actually a property OR an attribute of the <div> that it lives in. Text is generally handled as a separate text node in it’s own right. So when you were trying to do:

WebUI.getAttribute(findTestObject(“Page_Works Order Master File/div_janice”), “text”)

… it failed of course, because, as @Andrej_Podhajsky pointed out, this code will look for an attribute (or a property) called ‘text’, and try and get it’s value. Since the <div> does not have such an attribute, it returned null.


#12

Excellent post (as usual) Brandon.

One difficulty this smudging of attr/prop presents is with the value attribute. Katalon will ALWAYS retrieve the property, NEVER the attribute. If you want to verify the attribute, tough, you’ll need to write your own keyword to get at it.

Why would you want to access the attribute verbatim? To test (for example) whether form.reset is functioning correctly. Katalon can only tell you the current value of the value property, not the value of the attribute as set by the original http request of the server.

And that, my friends, is bad.


#13

Wow, that is definitely a problem… I’ve always thought that if it were going to be just one method, they should have called it getProperty() instead, because attribute key-value pairs are stored as a property anyway:

image

In fact, I’m pretty sure that getAttribute() operates solely against properties, and just checks the “attributes” property to find attribute values for a node. Maybe this is what you’re already doing, but I wonder if you could check the true value property:

image

against the “value” map entry in the attribute property:


#14

Interesting.

My approach is (as you know), move on, it is what it is, call JS where I need to, yada yada…

The other thing that makes this stand out is the masking of the value attribute when it’s undefined:

<form...>
  <input name="fred" type="text" and nothing else>
</form>

fred does not HAVE a value attribute. And web interface APIs have well known, published standards for how that should behave. Again, you can’t verify that OTB in KS.

You and I know, submitting that form would NOT send fred to the server. But based on results from KS, implied by those result, you might expect the value-pair to be fred="". Wrong.

And that too, my friends, is bad.

Anyway, like I said, moving on… :wink:


#15

I’m new to web applications and Katalon but I think I’m following this conversation. Right now, I’m trying to change the href attribute on an anchor tag . I’m doing this because I don’t want to go to the actual site, I want to go to a test site instead. I believe that all my code is correct, but when I enter the following command, it does not modify the ‘href’ attribute…

newObject=WebUI.modifyObjectProperty(oldObject, ‘href’, ‘equals’, newUrl, false)

The explanations above completely explains why. As a workaound, Instead of clicking the element, I’m just going to navigate to the newUrl.


#16

Hi Kevin

Be aware, we’re talking about raw HTML elements and their attrs/props. Don’t confuse that with Katalon’s TestObject wrappers.

Not saying this issue is not your issue… but I’m surprised you’re having problems with modifyObjectProperty.

Advice: Post a new Topic in Web Testing.