How to select a value for a dropdown field that reloads the entire page

Hi,

I want to select an option to one of my select field. The field is different from other select fields because it reloads the entire page when I select a value. Please help!!

This line in my code gives an error as

WebUI.selectOptionByValue(findTestObject('My TestObject'), 'MyValue', true)

Unable to select option by value 'MyValue' of object 'My TestObject' using regular expression (Root cause: org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

 

Thanks. But the above code also didn’t work. So I used selenium code instead & its working for now.

Here is the code I implemented…

new Select(WebUiCommonHelper.findWebElement(findTestObject('My TestObject'), 2)).selectByValue("MyValue")

Hi there,

Here is another workaround solution for your case, please give it another try. It will CLICK on the select object and select ‘MyValue’ option by sending that value as keys

WebUI.sendKeys(findTestObject('My TestObject'), 'MyValue')

Thanks

I tried the above code & the option is getting selected & saved.

However, after completing the

WebUiCommonHelper.selectOrDeselectOptionsByValue(new Select(webElement), 'MyValue', true, true, to, 'using regular expression log') step execution it logs the below error & never makes it to the wait step WebUI.waitForPageLoad(30)

Error Logged:-

Test Cases/Main Test Cases/SampleTests FAILED because (of) org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

Hi there,

In this case, it’s probably you have to write select steps to handle this special case. Unfortunately I don’t have any sample site to test, so hopefully this script will work for you:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import org.openqa.selenium.StaleElementReferenceException
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.Select

import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.common.WebUiCommonHelper
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

try {
WebUI.selectOptionByValue(findTestObject('My TestObject'), 'MyValue', true)
}
catch (StaleElementReferenceException e) {
TestObject to = findTestObject('My TestObject')
WebElement webElement = WebUiCommonHelper.findWebElement(to)
WebUiCommonHelper.selectOrDeselectOptionsByValue(new Select(webElement), 'MyValue', true, true, to, 'using regular expression log')

WebUI.waitForPageLoad(30)
}

It will reload when I select an item from the dropdown

Hi there,

One more additional question is: The page is reloaded when you click on the dropdown or when you select an item in the dropdown?

Thanks

That would be very helpful. But unfortunately we cannot share the logins for the product.

Hi there,

So looks like we will need to write custom handling in this case. Is your web application can be exposed so that I can test on it?

Thanks

The option is still not selected after completing that step execution…

Hi there,

Please don’t care about ‘Warning’ type, it’s just showing you warnings for current step behaviors. So after that step, the item you want is selected correctly on the browser or not?

Thanks

Okay… It still doesn’t save the value I selected & I still see the same problem. Due to modifications in the Failure handling, error got logged as Warning. But behavior is the same.

Hi there,

Please try to set ‘FailureHandling’ value to ‘Optional’ to continue in spite of any error during its execution.

WebUI.selectOptionByValue(findTestObject('My TestObject'), 'MyValue', true, FailureHandling.OPTIONAL)

Give it another try.

Thanks

Yeah. Still the same issue. Here is the log, see if this helps…

Object 'My Test Object' is clickable

End action : waitForElementClickable

Start action : selectOptionByValue

Finding web element with id: 'My Test Object' located by 'By.xpath: XPATH' in '30' second(s)

Found 1 web elements with id: 'My Test Object' located by 'By.xpath: XPATH' in '30' second(s)

Selecting options on object 'My Test Object' with value 'My Value' using regular expression

Selecting options on object 'My Test Object' with value 'My Value' using regular expression

Option at index '18' with value 'My Value' is selected using regular expression

Unable to select option by value 'My Value' of object 'My Test Object' using regular expression (Root cause: org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

Hi there,

Please try with ‘Wait For Element Clickable’ keyword and see how it goes. Is it still the same issue?

Thanks

Thanks for responding so quickly.

Yes, I did add the wait step before selecting, but it did not work. Also I tried to send Tab keys after. But of no luck.

I can see in the browser that it finds the object & the value, but it doesn’t get saved because the page reloads as soon as we have a value in there.

Hi there,

How about adding ‘Wait For Element Visible’ or ‘Wait For Element Present’ before selecting that option? This message ‘element is not attached to the page document’ is due to unavailability of an element being accessed by findelement method.

Thanks

Even a simple drop-down selection is susceptible to cause this issue (mismatched DOM probably often due to some overzealously page-rewriting JavaScript triggered by input / on change).

Wondering if instead of various workarounds (mostly WebUI.selectOptionByIndex(findTestObject(‘Page_Active User/select_Entry’), Entry, FailureHandling.CONTINUE_ON_FAILURE) for me - not documented for selectOptionByValue BTW, and never feeling good about overrides, of which OPTIONAL also would be one)…

Katalon’s APIs might introduce a more elegant way (maybe by checking whether the reference really has been made stale at all, or can safely be refreshed because its XPath still does match the updated document) to handle the StaleElementReferenceException (cf. also http://forum.katalon.com/discussion/5694/how-to-setup-test-runner-correctly-and-efficient) that had some history in Selenium too:
https://www.seleniumhq.org/exceptions/stale_element_reference.jsp
https://sqa.stackexchange.com/questions/3464/element-is-no-longer-attached-to-the-dom-staleelementreferenceexception-when-s
https://stackoverflow.com/questions/45002008/selenium-stale-element-reference-element-is-not-attached-to-the-page

https://stackoverflow.com/questions/28453068/how-to-fix-stale-element-reference-exception-while-trying-to-pick-date-from-da as well
(Strangely the Edit button for additions has become unavailable today with “Whoops! You need the Vanilla.Discussions.Edit permission to do that.”)

Hi

Is there a solution to this?

I have started learning the Katalon Studio - but got stuck almost immediately with “Select Option By Value”…

Here is the actual code:

WebUI.waitForElementVisible(findTestObject(‘BRONTO_ALL/select_Select a Sub-Account52’), 10)

WebUI.selectOptionByValue(findTestObject(‘BRONTO_ALL/select_Select a Sub-Account52’), ‘Financial Media Corp (finmc)’, true)

Here is the error i get:
Test Cases/BrontoSendMsg FAILED because (of) Unable to select option by value ‘Financial Media Corp (finmc)’ of object ‘Object Repository/BRONTO_ALL/select_Select a Sub-Account52’ using regular expression (Root cause: No option matched.)

If this is a different - please let me know and will post in the correct section.

Note: The same works fine with Katalon Recorder…

Thank you very much!
Andrew