In Katalon Studio, How to retrieve the values using the javascript executor. document.getElementsByTagName('input')[29].value

430 Views Asked by At

enter image description here

I tried below code but not works.

a = WebUI.executeJavaScript('document.getElementsByTagName("input")[29].value', null)

Thread.sleep(5000)

System.out.println(a)
1

There are 1 best solutions below

0
Mike Warren On

There is so much wrong with this question that I don't even know where to begin...

What are you trying to accomplish by using JavaScript (this is a testing code smell, for 99% of testing cases) to fetch a value ?

Why not do the following:

  • create a TestObject, preferably in the Object Repository, that points to the object in question.
  • give that Test Object the locator. This is, by default, some xpath.

In your case, give it xpath

(//input)[29]

. However, I advise you come up with a more meaningful selector for it (for example, select it by some class, data-* attribute, name) that is easier to maintain

WebUI.getAttribute(findTestObject('[whateverYourTestObjectNameIs]'), 'value')

// import statements here. Ctrl + Shift + O on your keyboard to bring those in

public final class GeneralWebUIUtils {

    public static final String Value = "value";

    public static final String GetValue(TestObject to) { 
        return WebUI.getAttribute(to, this.Value);
    }
}

Also, why are you pausing runtime by some hard-coded time amount? That is a testing code smell. Stop it!

What exactly are you waiting on? Use the WebUI keywords for this thing you are waiting on, and if none of those suffice, hmu and I may have the wait method you're looking for ....

Oh, and looking at that image you linked, it looks like you solved your own question.