Web Driver Find Element By

Hi guys…I really need help. I don’t know what happened.

So I was researching on the web and found the following scripts to search for a specific row in the table:

It worked like a charm before. But it is not working now with the error in console log “No such property: By for class: listingtable.searchListing”. It sounds like findElement(By…) is not working…

Here’s my searchListing code:

package listingtable

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import internal.GlobalVariable

import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import org.openqa.selenium.interactions.Actions as Actions
import com.kms.katalon.core.webui.common.WebUiCommonHelper as WebUiCommonHelper
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory

public class searchListing {

	@Keyword
	def searchFor(String item, int timeout) {
		'Expected value from Table'
		//String ExpectedValue = item

		WebDriver driver = DriverFactory.getWebDriver()
		WebElement tab = driver.findElement(By.cssSelector('//table[@class="k-selectable"]/tbody[1]'))
		List<WebElement> rows = tab.findElement(By.tagName('"tr"'))
		table: for (int i = 0; i <= rows.size(); i++) {
			List<WebElement> cols = rows.get(i).findElements(By.tagName('td'))
			for (int j = 0; j <= cols.size(); j++) {
				if (cols.get(j).getText().equalsIgnoreCase(item)) {
					cols.get(8).findElement(By.tagName('a')).click()

					break
				}//if
			}//for
		}//for
	}//keyword

}

Here’s the console log:

2018-12-11 18:03:55.323 e[39mDEBUGe[0;39m e[36mtestcase.Create New Employee (Import)    -e[0;39m e[39m37: delay(5)e[0;39m
2018-12-11 18:04:00.325 e[39mDEBUGe[0;39m e[36mtestcase.Create New Employee (Import)    -e[0;39m e[39m38: listingtable.searchListing.searchFor("emp030", 0)e[0;39m
2018-12-11 18:04:00.332 e[1;31mERRORe[0;39m e[36mk.k.c.m.CustomKeywordDelegatingMetaClass -e[0;39m e[31m❌ No such property: By for class: listingtable.searchListinge[0;39m
2018-12-11 18:04:00.344 e[1;31mERRORe[0;39m e[36mc.k.katalon.core.main.TestCaseExecutor   -e[0;39m e[31m❌ listingtable.searchListing.searchFor("emp030", 0) FAILED.e[0;39m
e[31mReason:e[0;39m
e[31mgroovy.lang.MissingPropertyException: No such property: By for class: listingtable.searchListinge[0;39m
e[31m	at listingtable.searchListing.searchFor(searchListing.groovy:37)e[0;39m
e[31m	at listingtable.searchListing.invokeMethod(searchListing.groovy)e[0;39m
e[31m	at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:49)e[0;39m
e[31m	at Create New Employee (Import).run(Create New Employee (Import):112)e[0;39m
e[31m	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)e[0;39m
e[31m	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:321)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:312)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:291)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:283)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:222)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:106)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:97)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)e[0;39m
e[31m	at TempTestCase1544522452451.run(TempTestCase1544522452451.groovy:22)e[0;39m
2018-12-11 18:04:00.345 e[1;31mERRORe[0;39m e[36mc.k.katalon.core.main.TestCaseExecutor   -e[0;39m e[31m❌ Test Cases/Create New Employee (Import) FAILED.e[0;39m
e[31mReason:e[0;39m
e[31mgroovy.lang.MissingPropertyException: No such property: By for class: listingtable.searchListinge[0;39m
e[31m	at listingtable.searchListing.searchFor(searchListing.groovy:37)e[0;39m
e[31m	at listingtable.searchListing.invokeMethod(searchListing.groovy)e[0;39m
e[31m	at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:49)e[0;39m
e[31m	at Create New Employee (Import).run(Create New Employee (Import):112)e[0;39m
e[31m	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)e[0;39m
e[31m	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:321)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:312)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:291)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:283)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:222)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:106)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:97)e[0;39m
e[31m	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)e[0;39m
e[31m	at TempTestCase1544522452451.run(TempTestCase1544522452451.groovy:22)e[0;39m
2018-12-11 18:04:00.372 e[34mINFO e[0;39m e[36mc.k.katalon.core.main.TestCaseExecutor   -e[0;39m e[39mEND Test Cases/C

Is it because of the recent update? Because this code worked before I update to version 5.9. I was not testing the case until recently after the update.

I have been working on this for two days. Help is very much needed and appreciated.

Thank youuuuuuuuuu!!!

import org.openqa.selenium.By

:wink:

2 Likes

OMG. Thank you!!! It works now. But how weird, how did it work before this? I did not import that library before but it worked?

Well, I don’t think it is possible. You had to remove the import line at some point, maybe during refactoring or organizing imports.

Anyways, to be sure that you have all required imports, you can use Ctrl + Shift + O or right click - Source - Organize imports.

2 Likes

Hmmm…but thank you so much on the help and tips!!