Dynamic Web Testing - Attempting to get HTML Attribute from Web Element using DriverFactory

Hi All,

This could be quite a complicated query for a first post, I have been working with the Scripting section of Katalon for a week or two now and finding it very useful. However I am currently attempting to find out if a Checkbox is enabled or disabled through using WebElement.

Background of the script:
The script dynamically goes through all Objects within the Object Repository and each item listed is looped through, therefore making it much easier for me to add multiple webpages going forward. The script then scrapes mostly just the text from each page and imports this into an excel spreadsheet, each webpage is given its own sheet so that it can be analysed easier going forward.

I am currently finding the tables on the web pages using xpath:

	String rowSeparator = 'tr'
	String columnSeparator = 'td'
	String varxpath = findTestObject(obj).findXpathValue('xpath:idRelative')
	
	WebDriver driver = DriverFactory.getWebDriver()
	WebElement Table = driver.findElement(By.xpath(varxpath))
	List<WebElement> rows_table = Table.findElements(By.tagName(rowSeparator))

I then loop through all the elements in the table:

	int rows_count = rows_table.size()
	for (int rownum = 0; rownum < rows_count; rownum++) {
		String ro = rows_count
		log.logInfo(ro)
		log.logInfo('New Row')
		List<WebElement> Columns_row = rows_table.get(rownum).findElements(By.tagName(columnSeparator))

Then do the same for the columns within the rows and get the text:
int columns_count = Columns_row.size()

		for (int columnnum = 0; columnnum < columns_count; columnnum++) {
			String celltext = Columns_row.get(columnnum).getText()

If the text isnt blank then it will add it to the cell or if it is blank I am trying to check if its a check box and then output the value. However I cannot seem to get any indicator to prove if the item within the column is a checkbox or not(as you can see I have tried many different attribute variants to see what could work):
if (celltext != ‘’) {
sheet.getRow(RowCount).createCell(4 + columnnum).setCellValue(celltext)
}
else {
if (columnnum == 0){
log.logInfo(‘First Cell Is Blank’)
String AttributeTest = Columns_row.get(columnnum).getAttribute(“class”)
log.logInfo(AttributeTest)
AttributeTest = Columns_row.get(columnnum).getAttribute(“type”)
log.logInfo(AttributeTest)
AttributeTest = Columns_row.get(columnnum).getAttribute(“input”)
log.logInfo(AttributeTest)
AttributeTest = Columns_row.get(columnnum).getAttribute(“div”)
log.logInfo(AttributeTest)
AttributeTest = Columns_row.get(columnnum).getAttribute(“checked”)
log.logInfo(AttributeTest)
List AttributeElements = Columns_row.get(columnnum).findElements(By.tagName(“input”))
log.logInfo(AttributeElements)
AttributeTest = AttributeElements.getAttribute(“checked”)
log.logInfo(AttributeTest)

					if (Columns_row.get(columnnum).getAttribute("type") == 'checkbox')
					{
						log.logInfo('Attribute is a checkbox')
						if (Columns_row.get(columnnum).getAttribute("checked") == 'checked') {
							sheet.getRow(RowCount).createCell(4 + columnnum).setCellValue('Allowed')
						}
						else
						{
							sheet.getRow(RowCount).createCell(4 + columnnum).setCellValue('Blocked')
						}
					}
					else {
						log.logInfo('Attribute is not a checkbox')
						
						//Uncomment once completed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
						//columnnum = columns_count
					}
					WebUI.delay(10, FailureHandling.STOP_ON_FAILURE)
				}
				
			}

The only Attribute that comes back with any data is “class” and this comes back as blank, all others show as null. Also currently “List AttributeElements = Columns_row.get(columnnum).findElements(By.tagName(“input”))” errors out, that was my most recent attempt at resolving my issue.

The HTML I am analysing is, in bold is what I am trying to identify:

Unknown URLs

Unknown Override File Extension
Unknown
Unknown URLs, Domains and IP Addresses
None SelectedAudio FilesCompressed FilesExecutable FilesVideo Files
Unknown YouTube
(Smart Play must be enabled in General > Video Filtering.)

Any help would be greatly appreciated, I am going to continue working on this and will update if I find a resolution to my issue. Let me know if anymore information is required or I am going down the wrong rabbit hole :smiley:

Hey Charlie

Yeah, you’re right… that’s a lot to follow. But before we get into the weeds on this…

.getText() returns the innerText of the node it is called upon. It does NOT return HTML. So if you’re expecting to find type=checkbox or similar, it isn’t going to work.

And getAttribute takes a TestObject, not the name of an HTML element.

Personally, I would probably use native web-tech for this: i.e. I’d write some JavaScript code and do my digging that way.

A quick update, I have since managed to find that I must loop through AttributeElements to get the attribute, but I am receiving a true whether or not the option is checked/unchecked:
List AttributeElements = Columns_row.get(columnnum).findElements(By.tagName(“input”))
//log.logInfo(AttributeElements)
int ellie_count = AttributeElements.size()
String ellie = ellie_count
log.logInfo(ellie)

					for (int ellienum = 0; ellienum < ellie_count; ellienum++) {
						AttributeTest = AttributeElements.get(ellienum).getAttribute("checked")
						log.logInfo(AttributeTest)
						if (AttributeTest == 'false')
						{
							WebUI.delay(60, FailureHandling.STOP_ON_FAILURE)
						}
					}

Hi Russ,

Thank you so much for the quick response, unfortunately it appears that I have a delay to post messages so I apologies for not acknowledging your response in the above message. I did post that about 5 minutes before you posted your response.

Since both of those messages I have resolved my issue. As per my last message, I found that I was required to loop through the elements once again and the value only returned True if the value existed, if the box was not checked it did not exist. Therefore I was able to place a check to see if the value was True or Null and therefore identify the difference that way:

						try {
							List<WebElement> AttributeElements = Columns_row.get(columnnum).findElements(By.tagName("input"))
							//log.logInfo(AttributeElements)
							int ellie_count = AttributeElements.size()
							String ellie = ellie_count
							log.logInfo(ellie)
						
							for (int ellienum = 0; ellienum < ellie_count; ellienum++) {
								String AttributeTest = AttributeElements.get(ellienum).getAttribute("checked")
								log.logInfo(AttributeTest)
								if (AttributeTest == null)
								{
									sheet.getRow(RowCount).createCell(4 + columnnum).setCellValue('Allowed')
								}
								else {
									sheet.getRow(RowCount).createCell(4 + columnnum).setCellValue('Blocked')
								}
							}
						}

This now outputs correctly into my spreadsheet after other pages are looped and reviewed. I only started using Katalon and Java/Groovy last week so its just a lot of trial and error at this point!

Thank you non the less for your very quick response and recommendation.

I do however have one potentially quite easy query in relation to troubleshooting using Katalon, I am using Keylogger as my main source of troubleshooting:
KeywordLogger log = new KeywordLogger()

However I find reviewing the Console can be quite tedious with the amount of logic I have going through it, I was wondering is there a way to highlight these in the Console to make it easier for me to read or a better method of outputting debugging information? Do you have any recommendations? If I need to post another topic I can do.

Yeah, should probably be another topic but here’s what I do.

Make the log view display as a list (not a tree).

KeywordLogger log = new KeywordLogger()
log.logWarning(" " + my_message) // that’s FOUR SPACES not one

Now your interesting things appear as warnings (which really doesn’t affect anything) and they’re indented from regular warnings.

Those two lines I add to a static method which I can call from anywhere/everywhere.

That post delay you’re seeing will disappear once you’ve posted… I dunno… 4,000 messages? :stuck_out_tongue_winking_eye:

Hi Russ,

Thank you so much for that! That is going to be so much easier to troubleshoot the many… many issues my code has during creation!

Ah, I am unsure if I will ever get to 4,000 messages so I guess I will just need to stick with the delay haha.

Hopefully my post may help others, but it could be a little niche. Strange how spending hours troubleshooting something bares no fruits, then as soon as you post on a forum it pops out of nowhere!

Regards,
Charlie.