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

Question

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

 

solved 0
How to 16 Answer 471 views

About the Author

Answers ( 16 )

  1. 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

    • Deepthi

      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.

       

  2. Hi there,

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

    Thanks

    • Deepthi

      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

  3. 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

    • Deepthi

      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.

      • This comment is edited.

        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

  4. Deepthi

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

  5. 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

  6. 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

  7. 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)
    }

    • Deepthi

      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

  8. This comment is edited.

    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

    • Deepthi

      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")

      Best answer

Leave an answer