Not able pick a value from a drop down list

import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.Select
import internal.GlobalVariable as GlobalVariable

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords

I’m not sure you need 100% of these imports, but with these you should get the webdriver, the driverfactory, the select, and the webelement.

Press ctrl + shift + o on your keyboard to automatically import everything. You may see a few modals pop up asking which class you want whenever there are multiple classes of the same name available. If this occurs, choose the ones that have “selenium” in their package path.

1 Like

HI Brandon,
After trying the set of code you advised i am getting the below errors.Please advise.

org.openqa.selenium.JavascriptException: Cannot click on option element. Executing JavaScript click function returned an unexpected error, but no error could be returned from Internet Explorer’s JavaScript engine.
Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:25:53’
System info: host: ‘TGGCTX095’, ip: ‘172.16.2.95’, os.name: ‘Windows Server 2008 R2’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: ‘1.8.0_181’
Driver info: com.kms.katalon.selenium.driver.CInternetExplorerDriver
Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, javascriptEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: false, ie.browserCommandLineSwitches: , ie.edgechromium: false, ie.edgepath: , ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: true, ignoreZoomSetting: true, initialBrowserUrl: http://localhost:43354/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: cc1bad1b-e851-4196-a3a6-8dc4db3d135c
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at com.kms.katalon.selenium.driver.CInternetExplorerDriver.execute(CInternetExplorerDriver.java:21)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement.lambda$new$0(EventFiringWebDriver.java:404)
at com.sun.proxy.$Proxy9.click(Unknown Source)
at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement.click(EventFiringWebDriver.java:417)
at org.openqa.selenium.support.ui.Select.setSelected(Select.java:311)
at org.openqa.selenium.support.ui.Select.setSelectedByIndex(Select.java:293)
at org.openqa.selenium.support.ui.Select.selectByIndex(Select.java:172)
at org.openqa.selenium.support.ui.ISelect$selectByIndex.call(Unknown Source)
at Create PO.run(Create PO:71)
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 TempTestCase1586641174573.run(TempTestCase1586641174573.groovy:23)

Your JavaScript code contains errors (or, maybe IE can’t understand it). The confusing this is, I don’t see any JavaScript in the code given to you by @Brandon_Hein.

When others are trying to help you, it’s important that…

  1. You don’t change the code in a substantive way to create a moving target for the person trying to help you.

  2. You show enough of the code you’re dealing with so the person trying to help you can consider all circumstances and potentials for error.

Hi Russ,
Sorry if my word hurts Brandon or Anybody.
Actually my script was not able to pick a value from a drop down list , and Brandon suggested some set of code .
So i added that set of code in my script and after running script , element was not clicked and the script failed,so i added the entire error to investigate,so that i was my aim.
i followed the instructions given by experts in this forum.
As i am new to katalon i am facing the difficuties and was seeking for help.
If my word hurts to anybody i am sorry.

You do not need to apologize. What you need to do is show us (the people trying to help you) ALL THE CODE relevant to the problem:

  1. The relevant HTML
  2. The relevant Test Case steps/code
  3. ANYTHING else you think might be relevant.

And don’t change anything without telling us.

You now have an error in JavaScript. This is a big surprise. Where did that JavaScript come from? Was it always part of your code? We need to see the relevant Test Case steps/code

If you have not read it before, please read this:

This is the entire code.

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 java.text.SimpleDateFormat as SimpleDateFormat
import org.openqa.selenium.By as By
import org.openqa.selenium.Keys as Keys
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import org.openqa.selenium.support.ui.Select as Select
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.configuration.RunConfiguration as RunConfiguration

WebUI.callTestCase(findTestCase('Login'), [:], FailureHandling.STOP_ON_FAILURE)

WebUI.click(findTestObject('Page_Merchandising/imgpt1eachSidebarItem2l1icon'))

WebUI.click(findTestObject('Page_Merchandising/span_Orders'))

WebUI.click(findTestObject('Page_Merchandising/span_Create Order'))

for (def rowNum = 1; rowNum <= findTestData('RMS/CreatePO').getRowNumbers(); rowNum++) {
    WebUI.click(findTestObject('Page_Create Order  Merchandising/input_pt1contentAreaReg0tabrg12supplierId'))

    WebUI.setText(findTestObject('Page_Create Order  Merchandising/input_pt1contentAreaReg0tabrg12supplierId'), findTestData(
            'RMS/CreatePO').getValue(1, rowNum))

    WebUI.sendKeys(findTestObject('Page_Create Order  Merchandising/input_pt1contentAreaReg0tabrg12supplierId'), Keys.chord(
            Keys.TAB))

    WebUI.delay(3)

    String format = 'MM/dd/yyyy'

    SimpleDateFormat sdf = new SimpleDateFormat(format)

    String date = sdf.format(new Date() + 4)


    WebUI.click(findTestObject('Page_Create Order  Merchandising/input_pt1contentAreaReg0tabrg12notBeforeDate'))

    WebUI.sendKeys(findTestObject('Page_Create Order  Merchandising/input_pt1contentAreaReg0tabrg12notBeforeDate'), date)

    WebUI.sendKeys(findTestObject('Page_Create Order  Merchandising/input_pt1contentAreaReg0tabrg12notBeforeDate'), Keys.chord(
            Keys.TAB))

    WebUI.delay(3)

    WebUI.sendKeys(findTestObject('Page_Create Order  Merchandising/input_pt1contentAreaReg0tabrg12notAfterDate'), date)

    WebUI.sendKeys(findTestObject('Page_Create Order  Merchandising/input_pt1contentAreaReg0tabrg12notAfterDate'), Keys.chord(
            Keys.TAB))

    WebUI.delay(3)

    WebUI.click(findTestObject('Page_Create Order  Merchandising/input_pt1contentAreaReg0tabrg12sbc1'))

	WebDriver driver = DriverFactory.getWebDriver()
	WebElement element = driver.findElement(By.xpath("//label[text()='Location']/preceding-sibling::select"))
	Select select = new Select(element)
	select.selectByIndex(5)
	

    
    WebUI.click(findTestObject('Page_Create Order  Merchandising/apt1contentAreaReg0tabrg12locationIdlovIconId'))

    
}

The HTML Code

The fields screenshot.
image

I have to select Store from location drop down list.
When recording or spying the element , katalon not able to capture the xpath or other attributes for the elements ( store , warehouse) available in the drop down list .It is only capturing the below attributes.

So it is still a mystery to me why you are getting an error saying “Executing JavaScript click function returned an unexpected error”. I don’t see any code that calls JavaScript.

That said, I’m going to give you some JavaScript to click the option elements. You can change which option element by altering the index - this code clicks the FIRST option element - nth-child(1) - change to 2, 3, 4 etc.

String js = '''
var opt = document.querySelector("#pt1:contentAreaReg:0:tabrg1:2:soc1::content option:nth-child(1)");
opt.click();
'''
WebUI.executeJavaScript(js, null)

Check I didn’t make a typo with that long id #pt1...

The code above is meant to replace…

image

The script failed again.Error message dispalying’ Unable to execute the Javascript

Reason:
com.kms.katalon.core.exception.StepFailedException: Unable to execute JavaScript.
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.ExecuteJavaScriptKeyword.executeJavascript(ExecuteJavascriptKeyword.groovy:42)
at com.kms.katalon.core.webui.keyword.builtin.ExecuteJavaScriptKeyword.execute(ExecuteJavascriptKeyword.groovy:37)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.executeJavaScript(WebUiBuiltInKeywords.groovy:3813)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$executeJavaScript$5.call(Unknown Source)
at Create PO.run(Create PO:73)
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 TempTestCase1586715107987.run(TempTestCase1586715107987.groovy:23)
Caused by: org.openqa.selenium.JavascriptException: Error from JavaScript: SyntaxError
Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:25:53’
System info: host: ‘TGGCTX095’, ip: ‘172.16.2.95’, os.name: ‘Windows Server 2008 R2’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: ‘1.8.0_181’
Driver info: com.kms.katalon.selenium.driver.CInternetExplorerDriver
Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, javascriptEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: false, ie.browserCommandLineSwitches: , ie.edgechromium: false, ie.edgepath: , ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: true, ignoreZoomSetting: true, initialBrowserUrl: http://localhost:30873/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: be409f62-258d-406d-b4d6-4f0f0dac990f
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at com.kms.katalon.selenium.driver.CInternetExplorerDriver.execute(CInternetExplorerDriver.java:21)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:489)
at org.openqa.selenium.support.events.EventFiringWebDriver.lambda$new$1(EventFiringWebDriver.java:105)
at com.sun.proxy.$Proxy8.executeScript(Unknown Source)
at org.openqa.selenium.support.events.EventFiringWebDriver.executeScript(EventFiringWebDriver.java:229)
at com.kms.katalon.core.webui.keyword.builtin.ExecuteJavaScriptKeyword$_executeJavascript_closure1.doCall(ExecuteJavascriptKeyword.groovy:48)
at com.kms.katalon.core.webui.keyword.builtin.ExecuteJavaScriptKeyword$_executeJavascript_closure1.call(ExecuteJavascriptKeyword.groovy)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
… 17 more

Sorry. My bad. Colon characters in the id must be escaped in CSS. And because the same escape character is used in JavaScript, it needs to be escaped again. :roll_eyes:

String js = '''
var opt = document.querySelector("#pt1\\:contentAreaReg\\:0\\:tabrg1\\:2\\:soc1\\:\\:content option:nth-child(1)");
opt.click();
'''
WebUI.executeJavaScript(js, null)

Hi Russ,
Encountered same error again.
Reason:
com.kms.katalon.core.exception.StepFailedException: Unable to execute JavaScript.
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.ExecuteJavaScriptKeyword.executeJavascript(ExecuteJavascriptKeyword.groovy:42)
at com.kms.katalon.core.webui.keyword.builtin.ExecuteJavaScriptKeyword.execute(ExecuteJavascriptKeyword.groovy:37)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.executeJavaScript(WebUiBuiltInKeywords.groovy:3813)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$executeJavaScript$5.call(Unknown Source)
at Create PO.run(Create PO:75)
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 TempTestCase1586772812269.run(TempTestCase1586772812269.groovy:23)
Caused by: org.openqa.selenium.JavascriptException: Error from JavaScript: SyntaxError
Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:25:53’
System info: host: ‘TGGCTX095’, ip: ‘172.16.2.95’, os.name: ‘Windows Server 2008 R2’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: ‘1.8.0_181’
Driver info: com.kms.katalon.selenium.driver.CInternetExplorerDriver
Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, javascriptEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: false, ie.browserCommandLineSwitches: , ie.edgechromium: false, ie.edgepath: , ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: true, ignoreZoomSetting: true, initialBrowserUrl: http://localhost:33149/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 5c92d50b-4a2f-4064-9494-1f8d01361e5b
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at com.kms.katalon.selenium.driver.CInternetExplorerDriver.execute(CInternetExplorerDriver.java:21)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:489)
at org.openqa.selenium.support.events.EventFiringWebDriver.lambda$new$1(EventFiringWebDriver.java:105)
at com.sun.proxy.$Proxy8.executeScript(Unknown Source)
at org.openqa.selenium.support.events.EventFiringWebDriver.executeScript(EventFiringWebDriver.java:229)
at com.kms.katalon.core.webui.keyword.builtin.ExecuteJavaScriptKeyword$_executeJavascript_closure1.doCall(ExecuteJavascriptKeyword.groovy:48)
at com.kms.katalon.core.webui.keyword.builtin.ExecuteJavaScriptKeyword$_executeJavascript_closure1.call(ExecuteJavascriptKeyword.groovy)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
… 17 more

document.querySelector("#pt1\\:contentAreaReg\\:0\\:tabrg1\\:2\\:soc1\\:\\:content option:nth-child(1)")

Try that in the browser console. Let me know what it does.

If it fails, let me know what this does:

document.querySelector("body")

Hi Russ ,
i have tried these two options
1.
var opt = document.querySelector("#pt1\:contentAreaReg\:0\:tabrg1\:2\:soc1\:\:content option:nth-child(1)");
console.log(opt.click()) ;

var opt = document.body.querySelector("#pt1\:contentAreaReg\:0\:tabrg1\:2\:soc1\:\:content option:nth-child(1)");

In both cases reason for failing is
com.kms.katalon.core.exception.StepFailedException: Unable to execute JavaScript.

Please let me know if there any syntax errors

1 and 2 is not what I asked you to do.

Please do what I asked you to do. Please post screenshots of the results.

hI ,i have added in teh browser console , please find the screenshot as attachedbelow.

Where is the page?

image

Load the page, THEN try the console code.


i was adding these two before and after page load

Now try to get the option you wanted by changing the number in nth-child (because nth-child(1) is empty).

The point is, the code works. When you put it into Groovy in your Test Case, MAKE SURE the page is ready and the select element is populated.

Hi Russ,
Still no luck .Getting same error " Unable to execute Javascript"
I tried also def location=WebUI.executeJavaScript(“document.querySelector(’#pt1\:contentAreaReg\:0\:tabrg1\:2\:soc1\:\:content option:nth-child(2)’).text”, null);
And then tried to click the text but same error every time.
No Idea why i am getting this error.

Try this:

WebUI.executeJavaScript("alert('upendras');", null)

The idea is to prove you can put some JS into your groovy and make the browser act on it.

The code I gave you before works - we proved it in the browser console. The error must be somewhere in the groovy code.