How to select xpath for a checkbox with dynamic ID and Name

How to select xpath for a checkbox with dynamic ID and Name?

        <input type="hidden" name="user._17092"><input type="checkbox" **name="user.17092" id="user.17092"** class="" style="">          



      <a href=".......11641/users/17092/edit" style="">john.ty+fordeletion@email1.com</a>



    </td>

xpath generated is → id(“user.17092”)

and this should be dynamic.

try this solutions

John said:

How to select xpath for a checkbox with dynamic ID and Name?

        <input type="hidden" name="user._17092"><input type="checkbox" **name="user.17092" id="user.17092"** class="" style="">          



      <a href=".......11641/users/17092/edit" style="">john.ty+fordeletion@email1.com</a>



    </td>

xpath generated is -> id(“user.17092”)

and this should be dynamic.

I assume you want to check/uncheck your checkbox.

Since it is changing you are not able to.

I have done it like this:

1. Import this into your script: import com.kms.katalon.core.testobject.ConditionType

2. use jquery to get values of your elemet attributes:

I cant check if this selectoris ok, but you can try it in your chrome console.

After you find right selector write it in your script

newName = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“name”)’, [])

newId = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“id”)’, [])

Pleas validate selector above.

After you get your values then you go to step 3

myTestObject = new TestObject(“customObject”)

List properties = new ArrayList()

properties.add(new TestObjectProperty(“name”, ConditionType.EQUALS, newName))

properties.add(new TestObjectProperty(“id”, ConditionType.EQUALS, newId))

//Set object

myTestObject.setProperties(properties)

//and finally check your box

WebUI.check(myTestObject)

xpath generated is -> id(“user.17092”)

I would suggest alternative XPath:

//input[starts-with(@id,"user.")]

thanks guys!

Could you take the screenshot of the area where the controls are placed? Is there any label modifying the input?

There is a built in function WebUI.modifyObjectProperty that you might be able to use as well. If you capture one of these checkboxes you can manipulate it to match what you need. You may need to find a way to pull that generated ID in order to insert it into the xpath. I will put an example below for you for something I did similar to this in order to dynamically get table row items that are dynamic.

WebUI.getText(WebUI.modifyObjectProperty(findTestObject("TinkerScriptObjects/itemData"), 'xpath', 'equals','id("Admin_' + badgeID + '")/div[@class="row"]/div[@class="col-md-6"]/table[@class="item-table table table-striped small"]/tbody[1]/tr[' + rowNumber + ']/td[@class="text-right"]', true))

Hello,

I have a page which displays list of individuals as checkbox that needs to be selected.
The id property for the first individual is in the form -ctl00_ContentPlaceHolder1_gvIndividualSearchList_ctl02_CheckBox1.
The individuals below will have ct03, ctl04 etc. I want to use a for loop to select the individuals which will select individual based on the current value of variable i in the loop

I could achieve this in selenium using the following manner -
driver.findElement(By.id(“ctl00_ContentPlaceHolder1_gvIndividualSearchList_ctl0”+i+"_CheckBox1")).click();

Could someone suggest the same in Katalon?

crokatalontest said:

John said:

How to select xpath for a checkbox with dynamic ID and Name?

        <input type="hidden" name="user._17092"><input type="checkbox" **name="user.17092" id="user.17092"** class="" style="">          



      <a href=".......11641/users/17092/edit" style="">john.ty+fordeletion@email1.com</a>



    </td>

xpath generated is -> id(“user.17092”)

and this should be dynamic.

I assume you want to check/uncheck your checkbox.

Since it is changing you are not able to.

I have done it like this:

1. Import this into your script: import com.kms.katalon.core.testobject.ConditionType

2. use jquery to get values of your elemet attributes:

I cant check if this selectoris ok, but you can try it in your chrome console.

After you find right selector write it in your script

newName = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“name”)’, [])

newId = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“id”)’, [])

Pleas validate selector above.

After you get your values then you go to step 3

myTestObject = new TestObject(“customObject”)

List properties = new ArrayList()

properties.add(new TestObjectProperty(“name”, ConditionType.EQUALS, newName))

properties.add(new TestObjectProperty(“id”, ConditionType.EQUALS, newId))

//Set object

myTestObject.setProperties(properties)

//and finally check your box

WebUI.check(myTestObject)

Hi,

Can you please explain step 2 and 3 in brief, and how to use of the following two lines:

newName = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“name”)’, [])

newId = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“id”)’, [])

_Can you please help me out?
_

Arun K Dey said:

crokatalontest said:

John said:

How to select xpath for a checkbox with dynamic ID and Name?

        <input type="hidden" name="user._17092"><input type="checkbox" **name="user.17092" id="user.17092"** class="" style="">          



      <a href=".......11641/users/17092/edit" style="">john.ty+fordeletion@email1.com</a>



    </td>

xpath generated is -> id(“user.17092”)

and this should be dynamic.

I assume you want to check/uncheck your checkbox.

Since it is changing you are not able to.

I have done it like this:

1. Import this into your script: import com.kms.katalon.core.testobject.ConditionType

2. use jquery to get values of your elemet attributes:

I cant check if this selectoris ok, but you can try it in your chrome console.

After you find right selector write it in your script

newName = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“name”)’, [])

newId = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“id”)’, [])

Pleas validate selector above.

After you get your values then you go to step 3

myTestObject = new TestObject(“customObject”)

List properties = new ArrayList()

properties.add(new TestObjectProperty(“name”, ConditionType.EQUALS, newName))

properties.add(new TestObjectProperty(“id”, ConditionType.EQUALS, newId))

//Set object

myTestObject.setProperties(properties)

//and finally check your box

WebUI.check(myTestObject)

Hi,

Can you please explain step 2 and 3 in brief, and how to use of the following two lines:

newName = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“name”)’, [])

newId = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“id”)’, [])

_Can you please help me out?
_

Sorry for my late response.

When you load your page that you are testing you need to right click on your checkbox (chrome or firefox browser) and select option INSPECT.
After that you can see html code of your element.
It is something like this:

Now if you look @ step 2 i got the value for “name” and i got value for “id”

newName will return SomeNAme
and newID will return SomeID

ID is unique and that means you only need to get ID.
You don’t need to fetch NAME value.

_newId = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“id”)’, [])
_newId will have value “SomeId” without quotes of course.

If you don’t get the NAME value than you need to skip this step:

properties.add(new TestObjectProperty(“name”, ConditionType.EQUALS, newName))

In step 3 now we create new test object (you don’t need to have your test object in object repository with this) and that new object is going to have
attributes of your checkbox.
And then you do whatever you need to do with this new object (click, check, uncheck whatever).

With this method you don’t need any object in your object repository.
You can do this for any object on your page.
You just need to get your element ID value, create new object, and do whatever you want to tho with it

And you don’t need to worry if your checkbox id (or any other id or any other attribute) constantly changes because you will always get the right value for “id” and “name” (or any other) attribute.

checkbox.PNG

crokatalontest said:

Arun K Dey said:

crokatalontest said:

John said:

How to select xpath for a checkbox with dynamic ID and Name?

        <input type="hidden" name="user._17092"><input type="checkbox" **name="user.17092" id="user.17092"** class="" style="">          



      <a href=".......11641/users/17092/edit" style="">john.ty+fordeletion@email1.com</a>



    </td>

xpath generated is → id(“user.17092”)

and this should be dynamic.

I assume you want to check/uncheck your checkbox.

Since it is changing you are not able to.

I have done it like this:

1. Import this into your script: import com.kms.katalon.core.testobject.ConditionType

2. use jquery to get values of your elemet attributes:

I cant check if this selectoris ok, but you can try it in your chrome console.

After you find right selector write it in your script

newName = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“name”)’, )

newId = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“id”)’, )

Pleas validate selector above.

After you get your values then you go to step 3

myTestObject = new TestObject(“customObject”)

List properties = new ArrayList()

properties.add(new TestObjectProperty(“name”, ConditionType.EQUALS, newName))

properties.add(new TestObjectProperty(“id”, ConditionType.EQUALS, newId))

//Set object

myTestObject.setProperties(properties)

//and finally check your box

WebUI.check(myTestObject)

Hi,

Can you please explain step 2 and 3 in brief, and how to use of the following two lines:

newName = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“name”)’, [])

newId = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“id”)’, [])

_Can you please help me out?
_

Sorry for my late response.

When you load your page that you are testing you need to right click on your checkbox (chrome or firefox browser) and select option INSPECT.
After that you can see html code of your element.
It is something like this:

Now if you look @ step 2 i got the value for “name” and i got value for “id”

newName will return SomeNAme
and newID will return SomeID

ID is unique and that means you only need to get ID.
You don’t need to fetch NAME value.

_newId = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“id”)’, )
_newId will have value “SomeId” without quotes of course.

If you don’t get the NAME value than you need to skip this step:

properties.add(new TestObjectProperty(“name”, ConditionType.EQUALS, newName))

In step 3 now we create new test object (you don’t need to have your test object in object repository with this) and that new object is going to have
attributes of your checkbox.
And then you do whatever you need to do with this new object (click, check, uncheck whatever).

With this method you don’t need any object in your object repository.
You can do this for any object on your page.
You just need to get your element ID value, create new object, and do whatever you want to tho with it

And you don’t need to worry if your checkbox id (or any other id or any other attribute) constantly changes because you will always get the right value for “id” and “name” (or any other) attribute.

Hello crokatalontest,

Thank you for your response. But when I applied these lines in my script then I faced error. Can you please check attached screen shot.

katalon_error.PNG

Did you add these two imports:

import com.kms.katalon.core.testobject.TestObjectProperty
import com.kms.katalon.core.testobject.ConditionType

Hi crokatalontest,

Thank you for your response. Yes, I did not import
import com.kms.katalon.core.testobject.TestObjectProperty

After import, my script is running but at line no. 13 syntax error (unable to execute java script) is coming. Could you please check the attached screen shot of error and manual test case?

1.

2.

I am waiting for your positive response thank you.

Log_viewer.jpg

test_case_manual.jpg

You need to escape the quotation marks.

Try this:

WebUI.executeJavaScript('return $("input[type=\'radio\']").attr("value")', [])

instead of:

WebUI.executeJavaScript('return $("input[type="radio"]").attr("value")', [])
2 Likes

Thank you team, now it is working.