How to check the Object while it is in the loop

Hi,
Can you please look into this code and suggest to proceed further ? Please find the query in the below code. I have the table in the web page and reading each cell and wanted to check the object with check box and I will enter the value if there is edit box. Here is the question is for checkbox.

Code :

WebDriver driver5 = DriverFactory.getWebDriver()
WebElement Table5 = driver5.findElement(By.xpath("//table[@id='mTAB1']/tbody"))
List<WebElement> rows_table5 = Table5.findElements(By.xpath("//*[starts-with(@class,'bgRow')]"))

int rows_count5 = rows_table5.size()
for(int z=0; z < rows_count5; z++) {
    List<WebElement> cols5 = rows_table5.get(z).findElements(By.tagName('td'))
    int cols_count5 = cols5.size()
    for (int colum5 = 0; colum5 < cols_count5; colum5++) {
         String celltext5 = cols5.get(colum5).getText()
	 WebUI.check(findTestObject('???'))  
    }
}

How do I check the check box here or do I need to
check the property name here ?

You already have the web elements, why would you try to switch to WebUI methods that use web objects?

Instead of WebUI.check(findTestObject('???')) use cols5[colum5].click().

Thanks for your quick reply. Can I use like below ? Please suggest !!
I see underline under the check and setText which means error.

  	int rows_count5 = rows_table5.size()
  	for(int z=0; z < rows_count5; z++) {
  		List<WebElement> cols5 = rows_table5.get(z).findElements(By.tagName('td'))
  		int cols_count5 = cols5.size()
  		for (int colum5 = 0; colum5 < cols_count5; colum5++) {
  			 String celltext5 = cols5.get(colum5).getText()
  			 if(colum5==1)
  		            cols5[colum5].check()
  			 if(colum5==3)
  			     cols5[colum5].setText("25")

You are writing Selenium code, that’s why you get underlined text. Those do not exist.

I suggested .click(), not .check() (this will try to click the preceding web element).
And, instead of .setText(), use .sendKeys().

1 Like

Thank You.
But, I am getting an error with missingmethodexception for cols5[colum5].sendKeys(“25”).
How to proceed further. please suggest.
cols5[colum5].click() is working fine.

Error Log:
2019-07-23 19:31:16.561 ERROR c.k.k.c.c.keyword.CucumberReporter - :x: it should open the View and Analyze Govt Bills windowsass FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: org.openqa.selenium.remote.RemoteWebElement.sendKeys() is applicable for argument types: (java.lang.Integer) values: [“25”]
Possible solutions: sendKeys([Ljava.lang.CharSequence;)
at AssignInquiry004.it_should_open_view_analyze_govt_bills_windows(AssignInquiry004.groovy:317)
at ✽.it should open the View and Analyze Govt Bills windowsass(C:/Users/vdavuluri2/Katalon Studio/Govt Inquiry/Include/features/Assign Inquiry-004.feature:29)

cols5[colum5].sendKeys("25")

Be certain the value "25" has quotes.

I tried that also… still same error…

it says the possible solution in the error log…
Possible solutions: sendKeys([Ljava.lang.CharSequence;)
at AssignInquiry004.it_should_open_view_analyze_govt_bills_windows(AssignInquiry004.groovy:318)
at ✽.it should open the View and Analyze Govt Bills windowsass(C:/Users/vdavuluri2/Katalon Studio/Govt Inquiry/Include/features/Assign Inquiry-004.feature:29)

Thank You. I have tried this and not working. Do I need to import anything ?

Sit tight. Selenium wiz (otherwise known as @Brandon_Hein) is on his way.

Just for my sanity, I’ve renamed and simplified some things in the below code:

WebDriver driver = DriverFactory.getWebDriver();
List<WebElement> rows = driver.findElements(By.xpath("//table[@id='mTAB1']/tbody//*[starts-with(@class,'bgRow')]"));

for(WebElement row : rows) {
    List<WebElement> cells = row.findElements(By.tagName('td'));
    for (int i = 0; i < cells.size(); i++) {
          if(i == 1) {
              cells.get(i).click();
              break;
          }
          if(i == 3) {
              cells.get(i).sendKeys(“25”);
          }
    }
}

You should only need a couple of imports to make this work, but instead of typing those out, there’s a really handy way to manage imports. On your keyboard, press ctrl + shift + o, which should import everything you need automatically.

Also of note: you are trying to click a checkbox or send keys to a field, yet I don’t see anywhere where you are locating an <input>/<textarea> element to manipulate (you are still only referencing a <td> element, which is just a cell in the table). Please give this code a try and let me know.

1 Like

Thanks for your reply. I have tried the above code. But it is not working. It is not checked the check box also. I have used the keys ctrl+shift+o,

Regarding the / that you mentioned, how to code these lines ? Please suggest to proceed further.

Can you share the HTML for the checkbox and the text field?

Thank You for your reply. Please find the below HTML code.

<table width=“100%” cellspacing=“0” border=“0” class=“wrap”>
<tr>
<!-- InstanceBeginEditable name=“PageContent” –>
<td width=“100%” class=“grid”>
<!–************ uitmplbegin: mTIE ******–>
<table width=“100%” cellspacing=“0” border=“0” class=“mTIE” id=“mTIE1”>
<!–
begin:column header ******–>
<tr class=“headerRow”>
<th scope=“col” id=“h1”>
<!-- DO NOT REMOVE THIS COMMENT –>
<div id=“checkAllBtn”>
<input type=“button” class=“checkBtn” title=“Check All” value=“Check All” onclick=“uitmpl_chkBoxSelect(this.form,‘checkAllBtn’,‘clearAllBtn’);toggle()” onkeypress=“if(chkKey(event)){uitmpl_chkBoxSelect(this.form,‘checkAllBtn’,‘clearAllBtn’);toggle()}” />
</div>

<div id=“clearAllBtn”>
<input type=“button” class=“checkBtn” title=“Clear All” value=“Clear All” onclick=“uitmpl_chkBoxClear(this.form,‘checkAllBtn’,‘clearAllBtn’);toggle()” onkeypress=“if(chkKey(event)){uitmpl_chkBoxClear(this.form,‘checkAllBtn’,‘clearAllBtn’);toggle()}” />
</div>
</th>
<th scope=“col” id=“h2”>Assign User</th>
<th scope=“col” id=“h3”>Assign %</th>
</tr>

<tr class=“bgRow1”>
<td headers=“h1” align=“center”>
<input type=“checkbox” class=“checkbox” name=“chk_user” value=“bvenkatdi2” onclick=“toggle()” onkeypress=“if(chkKey(event)){toggle()}” title=“Select User Veerareddy Venkat” />
</td>
<td headers=“h2”>
Veerareddy Venkat
</td>
<td headers=“h3”>
<input type=“text” title=“Enter Numeric Value” class=“decimal” name=“assigned_disputes_percent” value=“0” onblur=“compute ()” disabled/>
</td>
</tr>

<tr class=“bgRow2”>
<td headers=“h1” align=“center”>
<input type=“checkbox” class=“checkbox” name=“chk_user” value=“ct1529” onclick=“toggle()” onkeypress=“if(chkKey(event)){toggle()}” title=“Select User Charet Talastas” />
</td>
<td headers=“h2”>
Charet Talastas
</td>
<td headers=“h3”>
<input type=“text” title=“Enter Numeric Value” class=“decimal” name=“assigned_disputes_percent” value=“0” onblur=“compute ()” disabled/>
</td>
</tr>

<tr class=“bgRow1”>
<td headers=“h1” align=“center”>
<input type=“checkbox” class=“checkbox” name=“chk_user” value=“gt3232” onclick=“toggle()” onkeypress=“if(chkKey(event)){toggle()}” title=“Select User GEORGE TERRY” />
</td>
<td headers=“h2”>
GEORGE TERRY
</td>
<td headers=“h3”>
<input type=“text” title=“Enter Numeric Value” class=“decimal” name=“assigned_disputes_percent” value=“0” onblur=“compute ()” disabled/>
</td>
</tr>

<tr class=“bgRow2”>
<td headers=“h1” align=“center”>
<input type=“checkbox” class=“checkbox” name=“chk_user” value=“la3929” onclick=“toggle()” onkeypress=“if(chkKey(event)){toggle()}” title=“Select User Anderson Lauren” />
</td>
<td headers=“h2”>
Anderson Lauren
</td>
<td headers=“h3”>
<input type=“text” title=“Enter Numeric Value” class=“decimal” name=“assigned_disputes_percent” value=“0” onblur=“compute ()” disabled/>
</td>
</tr>

<tr class=“bgRow1”>
<td headers=“h1” align=“center”>
<input type=“checkbox” class=“checkbox” name=“chk_user” value=“rm0013” onclick=“toggle()” onkeypress=“if(chkKey(event)){toggle()}” title=“Select User Veerareddy V” />
</td>
<td headers=“h2”>
Veerareddy V
</td>
<td headers=“h3”>
<input type=“text” title=“Enter Numeric Value” class=“decimal” name=“assigned_disputes_percent” value=“0” onblur=“compute ()” disabled/>
</td>
</tr>

<tr class=“bgRow2”>
<td headers=“h1” align=“center”>
<input type=“checkbox” class=“checkbox” name=“chk_user” value=“rm0014” onclick=“toggle()” onkeypress=“if(chkKey(event)){toggle()}” title=“Select User V Venkat” />
</td>
<td headers=“h2”>
V Venkat
</td>
<td headers=“h3”>
<input type=“text” title=“Enter Numeric Value” class=“decimal” name=“assigned_disputes_percent” value=“0” onblur=“compute ()” disabled/>
</td>
</tr>

<tr class=“bgRow1”>
<td headers=“h1” align=“center”>
<input type=“checkbox” class=“checkbox” name=“chk_user” value=“blaurendi2” onclick=“toggle()” onkeypress=“if(chkKey(event)){toggle()}” title=“Select User Smith Kelly” />
</td>
<td headers=“h2”>
Smith Kelly
</td>
<td headers=“h3”>
<input type=“text” title=“Enter Numeric Value” class=“decimal” name=“assigned_disputes_percent” value=“0” onblur=“compute ()” disabled/>
</td>
</tr>

<tr id=“totalRow”>
<td headers=“h1” colspan=“2”>Total</td>
<td headers=“h3”>
<input type=“text” class=“decimal” name=“total_assigned_disputes_percent” title=“Total Assigned Disputes Percent” value=“” readonly>
</td>
<tr>
</table>

So like I said above, you are still only referencing a cell in the table. You are not locating the actual checkbox/input field. To do this, try:

WebDriver driver = DriverFactory.getWebDriver();
List<WebElement> rows = driver.findElements(By.xpath("//table[@id='mTAB1']/tbody//*[starts-with(@class,'bgRow')]"));

for(WebElement row : rows) {
    List<WebElement> cells = row.findElements(By.tagName('td'));
    for (int i = 0; i < cells.size(); i++) {
          if(i == 1) {
              cells.get(i).findElement(By.xpath(“//input”)).click();
              break;
          }
          if(i == 3) {
              cells.get(i).findElement(By.xpath(“//input”)).sendKeys(“25”);
          }
    }
}

Appreciate your help. Thanks a lot. :+1:

Has your issue been resolved?

Thank You, It is resolved. :+1: