Trouble Getting Element By Id

I am trying to get the contents of the element circled in red on the screen below.

I have tried the various versions of script that you see below but they have all failed.

 //IWebElement expectedModifier = driver.findElement(By.xpath("//input[@type='text' and contains(@id,'proc2Mod1')]")).getText();

//String expectedModifier = WebUI.getAttribute(findTestObject('Page_White Plume Technologies AccelaCAPTURE 61/proc2Mod1'), 'value')

//IWebElement expectedModifier = driver.findElement(By.xpath("//input[contains(@id,'proc2Mod1')]")).getText();

//String expectedModifier = WebUI.getAttribute(findTestObject('Page_White Plume Technologies AccelaCAPTURE 61/proc2Mod1'), 'value')

String expectedModifier = driver.findElement(By.xpath("//input[@id='proc2Mod1']")).getText();

But I continue to get the error below;

Test Cases/InsertAfter Lightning Rule FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: Unable to get attribute
	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)

The html for the element I am trying to get the text value for is below;

Thanks

What is the result of executing the line above ? Please paste the full console log. Try to put a WebUI.delay(5) before getting the attributes to eliminate timing problems. Also, it would help to provide the HTML of the element (and its surroundings) by inspecting the element with Inspection Tool for more context.

Below is the error from the console log;
2020-05-06 12:40:59.247 DEBUG testcase.InsertLast Test Case - 23: delay(5)
2020-05-06 12:41:04.277 DEBUG testcase.InsertLast Test Case - 24: expectedModifier = getAttribute(findTestObject(“Page_White Plume Technologies AccelaCAPTURE 61/proc2Mod1”), “value”)
2020-05-06 12:41:04.280 WARN c.k.k.core.testobject.ObjectRepository - Test object with id ‘Object Repository/Page_White Plume Technologies AccelaCAPTURE 61/proc2Mod1’ does not exist
2020-05-06 12:41:04.293 WARN c.k.k.core.testobject.ObjectRepository - Test object with id ‘Object Repository/’ does not exist
2020-05-06 12:41:04.457 ERROR c.k.k.core.keyword.internal.KeywordMain - :x: Unable to get attribute (Root cause: com.kms.katalon.core.exception.StepFailedException: Unable to get attribute
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:26)
at com.kms.katalon.core.webui.keyword.builtin.GetAttributeKeyword.getAttribute(GetAttributeKeyword.groovy:93)
at com.kms.katalon.core.webui.keyword.builtin.GetAttributeKeyword.execute(GetAttributeKeyword.groovy:68)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.getAttribute(WebUiBuiltInKeywords.groovy:947)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$getAttribute$4.call(Unknown Source)
at InsertLast Test Case.run(InsertLast Test Case:84)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1588786840410.run(TempTestCase1588786840410.groovy:25)
Caused by: java.lang.IllegalArgumentException: Object is null
at com.kms.katalon.core.helper.KeywordHelper.checkTestObjectParameter(KeywordHelper.java:33)
at com.kms.katalon.core.webui.keyword.builtin.GetAttributeKeyword$_getAttribute_closure1.doCall(GetAttributeKeyword.groovy:77)
at com.kms.katalon.core.webui.keyword.builtin.GetAttributeKeyword$_getAttribute_closure1.call(GetAttributeKeyword.groovy)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
at com.kms.katalon.core.webui.keyword.builtin.GetAttributeKeyword.getAttribute(GetAttributeKeyword.groovy:93)
at com.kms.katalon.core.webui.keyword.builtin.GetAttributeKeyword.execute(GetAttributeKeyword.groovy:68)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.getAttribute(WebUiBuiltInKeywords.groovy:947)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$getAttribute$4.call(Unknown Source)
at Script1588783954154.run(Script1588783954154.groovy:84)
… 11 more
)
2020-05-06 12:41:04.458 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/InsertLast Test Case FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: Unable to get attribute
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:26)
at com.kms.katalon.core.webui.keyword.builtin.GetAttributeKeyword.getAttribute(GetAttributeKeyword.groovy:93)
at com.kms.katalon.core.webui.keyword.builtin.GetAttributeKeyword.execute(GetAttributeKeyword.groovy:68)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.getAttribute(WebUiBuiltInKeywords.groovy:947)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$getAttribute$4.call(Unknown Source)
at InsertLast Test Case.run(InsertLast Test Case:84)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1588786840410.run(TempTestCase1588786840410.groovy:25)
Caused by: java.lang.IllegalArgumentException: Object is null
at com.kms.katalon.core.helper.KeywordHelper.checkTestObjectParameter(KeywordHelper.java:33)
at com.kms.katalon.core.webui.keyword.builtin.GetAttributeKeyword$_getAttribute_closure1.doCall(GetAttributeKeyword.groovy:77)
at com.kms.katalon.core.webui.keyword.builtin.GetAttributeKeyword$_getAttribute_closure1.call(GetAttributeKeyword.groovy)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
… 17 more

2020-05-06 12:41:04.460 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/InsertLast Test Case
2020-05-06 12:41:04.460 INFO c.k.katalon.core.main.TestCaseExecutor - =================== Smart XPath Report =======================
2020-05-06 12:41:04.461 INFO c.k.katalon.core.main.TestCaseExecutor - Refer to the link below to preview and approve auto healing on broken test objects.
2020-05-06 12:41:04.461 INFO c.k.katalon.core.main.TestCaseExecutor - https://docs.katalon.com/katalon-studio/docs/auto-healing-smart-xpath.html
2020-05-06 12:41:04.461 INFO c.k.katalon.core.main.TestCaseExecutor - ===============================================================

Below is the HTML of the element and its surroundings.

Thanks Again

hi,

you could try with css locator (CSS Selector) also

This line will never work:

String expectedModifier = driver.findElement(By.xpath("//input[@id='proc2Mod1']")).getText();

Your <input id="proc2Mod1" ...> does not have any innerText so getText() will not work.

In addition, that code will not and cannot report a “cannot get attribute” error. It does not work like that. PLEASE be clear when making requests for help and make sure you’re reporting exact errors for the exact same code you’re posting and having trouble with.

Your topic title says: Trouble getting element by id.
Question - what do you mean by “getting” the element? It looks as though you want to get the content of the element, in this case the value of an input element. Correct?

Here’s some JavaScript that will get the value. Make sure the input element is present in the DOM, then…

String js = 'return document.querySelector("#proc2Mod1").value;'
String val = WebUI.executeJavaScript(js, null)
println val

Again, make sure your page is ready and populated and the input element is present.

1 Like

That worked great. Thank you not only for the solution but for the great explanation.

1 Like

In case you are wondering why the error was thrown, it was because the Test Object is null, possibly you have an invalid Test Object ID which doesn’t allow Katalon to retrieve the actual Test Object. You should get that checked.