Not able to select a drop down value using selectOptionByIndex

kazurayam said:

Jacqueline,

Could you see my study above? I suppose you can derive a solution for yourself.

Hi,

I tried but it doesn’t work and I got different error after that. Could you please create a valid xpath based on the html? Thanks.

  • - Please Select -
  • A
  • B
  • C
  • D
  • E

kazurayam said:

Tamanna,

Please find my proposal :
https://github.com/kazurayam/KatalonDiscussion14605

I assumed that your HTML has such lines:

<select name="something">
<option id="ui-select-choices-row-88-234">
    <span class="ui-select-choices-row-inner" uis-translude-append>
        <div ng-transclude="item" class="ng-score">
            <div ng-bind-html="item[vm.labelField]" class="ng-binding">foo</div>
        </div>
    </span>
</option>
<option id="ui-select-choices-row-99-3">
    <span class="ui-select-choices-row-inner" uis-translude-append>
        <div ng-transclude="item" class="ng-score">
            <div ng-bind-html="item[vm.labelField]" class="ng-binding">bar</div>
        </div>
    </span>
</option>
<option id="ui-select-choices-row-24-2">
    <span class="ui-select-choices-row-inner" uis-translude-append>
        <div ng-transclude="item" class="ng-score">
            <div ng-bind-html="item[vm.labelField]" class="ng-binding">Alternative Dispute Resolution</div>
        </div>
    </span>
</option>
> ``` > > And I assumed that you want to grasp the element with id "ui-select-choices-row-24-2", you want to abstract 24 portion, and you want to regard 2 as the key for lookup. > > I made a test object which as a locator as follows: > > ``` > //option[starts-with(@id,'ui-select-choices-row-') and substring-after(substring-after(@id,'ui-select-choices-row-'),'-')="${index}"] > ``` > > Well, it is a bit complicated. This is a valid XPath expression. It just works. It is inevitably complicated because your requirement deserves. > > My test case has following lines: > > ``` > def content = WebUI.getText(findTestObject('Page_14605/option_with_param', ['index':'2'])) WebUI.verifyEqual(content, 'Alternative Dispute Resolution') > ``` > > This test case runs successful. > >

Hi,

Thanks for the answer. I tried your code, it dint work at first then i replaced ‘option’ in xpath with *, as It seemed to me that element name that i wanted to catch is not ‘option’ but something more complex so i used * instead and it worked!

Thank you for the help, i was stuck at it for almost 4 days! :slight_smile:

Jacqueline,

Could you please create a valid xpath based on the html?

Still I do not understand what you want to do. Could you please describe which part of HTML fragment you want to select? Are you satisfied if I regard the data-offset-index attribute as the parameter to Test Object?

kazurayam said:

Jacqueline,

Could you please create a valid xpath based on the html?

Still I do not understand what you want to do. Could you please describe which part of HTML fragment you want to select? Are you satisfied if I regard the data-offset-index attribute as the parameter to Test Object?

Hi Kazurayam,

I would like to select a value from the drop-down list but I always get an error on “Element should have been “select” but was “span”” or “Element not visible”. If I copy the xPath from the inspect element it doesn’t work either.

May I know how should I go about creating the xPath for the selection? Thanks.

Drop-Down.png

Jaqueline,

In the Inspector screen, could you expand the following ?

I expect that the would contain the list of names “BAIC”, “BMW”, … but I am not sure. I need to be sure where the list is contained in the HTML.

span_k-icon.PNG

kazurayam said:

Jaqueline,

In the Inspector screen, could you expand the following ?

I expect that the would contain the list of names “BAIC”, “BMW”, … but I am not sure. I need to be sure where the list is contained in the HTML.

Span class.JPG

Jaquline,

Thank you for additional screenshot.

I can not find ‘BAIC’ and ‘BMW’ in the document node tree. Do you have any idea why? Where is the text ‘BAIC’?

kazurayam said:

Jaquline,

Thank you for additional screenshot.

I can not find ‘BAIC’ and ‘BMW’ in the document node tree. Do you have any idea why? Where is the text ‘BAIC’?

Test.JPG

Jaqueline,

I made a running example. Please have a look at this:
https://github.com/kazurayam/KatalonDiscussionComment14771

I made a Test Object like this:

//ul[@id='ManufacturerList_listbox']/li[@data-offset-index='${index}']

I made a Test Case like this:

WebUI.openBrowser('')WebUI.navigateToUrl('http://demoaut-mimic.kazurayam.com/comment14771_testbed.html')// The target page is Ajax-driven. Page is dynamically updated by JavaScript.
// We need to wait for some microseconds until the page become completed.
// Let's wait for the '- Please Select -' text become visible.
WebUI.waitForElementVisible(
	findTestObject('Page_14771/li_- Please Select -'), 20)def contentA = WebUI.getText(
	findTestObject('Page_14771/li_- Please Select -'))
WebUI.verifyEqual(contentA, '- Please Select -')def contentB = WebUI.getText(
	findTestObject('Page_14771/li_ManufacturerList_BAIC'))
WebUI.verifyEqual(contentB, 'BAIC')def content1 = WebUI.getText(
	findTestObject('Page_14771/li_ManufacturerList_parameterized',
		['index':'1']))
WebUI.verifyEqual(content1, 'BAIC')def content2 = WebUI.getText(findTestObject(
	'Page_14771/li_ManufacturerList_parameterized',
	['index':'2']))
WebUI.verifyEqual(content2, 'BMW')// If we want to know the size of the ManufacturerList,
// look at the data-offset-index attribute of the last <li> element.
def lastIndex = WebUI.getAttribute(
	findTestObject('Page_14771/li_ManufacturerList_last'),
	'data-offset-index')
WebUI.verifyEqual(lastIndex, '4')WebUI.closeBrowser()

You mentioned you are suffering from message “Element not visible”. Most probably that is because your Application-Under-Test is a Ajax-driven website where HTML is dynamically updated by JavaScript in the browsers. In order for the Test Case run in sync with Ajax-processing, the Test Case should wait for the target element become visible. This synchronization is done by this line:

WebUI.waitForElementVisible(
    findTestObject('Page_14771/li_- Please Select -'), 20)

kazurayam said:

Jaqueline,

I made a running example. Please have a look at this:
https://github.com/kazurayam/KatalonDiscussionComment14771

I made a Test Object like this:

//ul[@id='ManufacturerList_listbox']/li[@data-offset-index='${index}']

I made a Test Case like this:

WebUI.openBrowser('')WebUI.navigateToUrl('http://demoaut-mimic.kazurayam.com/comment14771_testbed.html')// The target page is Ajax-driven which page is dynamically updated by JavaScript.

// We need to wait until the page is displayed.
// Let’s wait for ‘- Please Select -’ become visible.
WebUI.waitForElementVisible(
findTestObject(‘Page_14771/li_- Please Select -’), 20)def contentA = WebUI.getText(
findTestObject(‘Page_14771/li_- Please Select -’))
WebUI.verifyEqual(contentA, ‘- Please Select -’)def contentB = WebUI.getText(
findTestObject(‘Page_14771/li_ManufacturerList_BAIC’))
WebUI.verifyEqual(contentB, ‘BAIC’)def content1 = WebUI.getText(
findTestObject(‘Page_14771/li_ManufacturerList_parameterized’,
[‘index’:‘1’]))
WebUI.verifyEqual(content1, ‘BAIC’)def content2 = WebUI.getText(findTestObject(
‘Page_14771/li_ManufacturerList_parameterized’,
[‘index’:‘2’]))
WebUI.verifyEqual(content2, ‘BMW’)// If we want to know the size of the ManufacturerList,
// look at the data-offset-index attribute of the last

  • element.
    def lastIndex = WebUI.getAttribute(
    findTestObject(‘Page_14771/li_ManufacturerList_last’),
    ‘data-offset-index’)
    WebUI.verifyEqual(lastIndex, ‘4’)WebUI.closeBrowser()
    
      
      
    You mentioned you are suffering from message "Element not visible". That is because your Application-Under-Test is a Ajax-driven website where HTML is dynamically updated by JavaScript in the browsers. In order for the Test Case run in sync with Ajax-processing, the Test Case should wait for the target element become visible. This synchronization is done by this line:  
    
    

    WebUI.waitForElementVisible(
    findTestObject(‘Page_14771/li_- Please Select -’), 20)

    
      
    

    Hi,

    Just asking may I know what should be the xPath for these test objects also? Thanks.

    1. li_- Please Select -
    2. li_ManufacturerList_BAIC
    3. li_ManufacturerList_last
  • Just asking may I know what should be the xPath for these test objects also? Thanks.

    You can find them here:

    kazurayam said:

    Just asking may I know what should be the xPath for these test objects also? Thanks.

    You can find them here:
    https://github.com/kazurayam/KatalonDiscussionComment14771/tree/master/Object%20Repository/Page_14771

    Hi,

    I’ve got these errors when I run the script:

    1. Object ‘Object Repository/Page_Supplier Label Printing/li_- Please Select -’ is not visible after 20 second(s)
    2. Unable to verify equal between actual object ‘’ and expected object ‘- Please Select -’ (Root cause: Actual object ‘’ and expected object ‘- Please Select -’ are not equal)
    3. Test Cases/Create Preference FAILED because (of) Unable to verify equal between actual object ‘’ and expected object ‘- Please Select -’ (Root cause: Actual object ‘’ and expected object ‘- Please Select -’ are not equal)

    1 Like

    Jacqueline,

    1. Object ‘Object Repository/Page_Supplier Label Printing/li_- Please Select -’ is not visible after 20 second(s)

    I am sorry, I can not investigate this because I do not have access to your Web app.

    Let me tell you what, I presume, is happening in your Web app.

    When the target page is loaded and opened on browser window, the ul[@id=‘ManufacturersList_listbox’] element is empty (no BAIC there, no BMW in the list). After that when you click or move mouse over some HTML element, JavaScript reacts to fill the list with the names of manufacturers. This is the way how AJAX web pages works. A Ajax-driven web page transforms itself in response to the human action (click, mouse over, etc). Possibly your Test Case lacks triggering AJAX processing. Therefore the ManufacturersList actually is left empty. So that the test case finds that the “li_- Please Select -” object is not visible after 20 seconds.

    Possibly you need to

    1. find out which HTML element to click (or move mouse over it) and
    2. write a few lines of code in your TestCase to click the element in order for triggering AJAX processing.

    In order to accomplish this, you need to observe your Web page in depth and find out how the page works. Only those who have unrestricted access to the Application-Under-Test would be able to solve this sort of problem. AJAX-driven Web app is easy for visitors but is hard to test.

    Hi Kazurayam,

    I think you are right with the explanation. So, the workaround that I did is to click on the element first followed by waiting for the element to be visible then click on the chosen element “BAIC”. And it works! Thanks for your help. :slight_smile:

    kazurayam said:

    Tamanna,

    Please find my proposal :
    GitHub - kazurayam/KatalonDiscussion14605: parameterized Test Object

    I assumed that your HTML has such lines:

    <select name="something">
    
    <option id="ui-select-choices-row-88-234">
        <span class="ui-select-choices-row-inner" uis-translude-append>
            <div ng-transclude="item" class="ng-score">
                <div ng-bind-html="item[vm.labelField]" class="ng-binding">foo</div>
            </div>
        </span>
    </option>
    <option id="ui-select-choices-row-99-3">
        <span class="ui-select-choices-row-inner" uis-translude-append>
            <div ng-transclude="item" class="ng-score">
                <div ng-bind-html="item[vm.labelField]" class="ng-binding">bar</div>
            </div>
        </span>
    </option>
    <option id="ui-select-choices-row-24-2">
        <span class="ui-select-choices-row-inner" uis-translude-append>
            <div ng-transclude="item" class="ng-score">
                <div ng-bind-html="item[vm.labelField]" class="ng-binding">Alternative Dispute Resolution</div>
            </div>
        </span>
    </option>
    
    > ``` > > And I assumed that you want to grasp the element with id "ui-select-choices-row-24-2", you want to abstract 24 portion, and you want to regard 2 as the key for lookup. > > I made a test object which as a locator as follows: > > ``` > //option[starts-with(@id,'ui-select-choices-row-') and substring-after(substring-after(@id,'ui-select-choices-row-'),'-')="${index}"] > ``` > > Well, it is a bit complicated. This is a valid XPath expression. It just works. It is inevitably complicated because your requirement deserves. > > My test case has following lines: > > ``` > def content = WebUI.getText(findTestObject('Page_14605/option_with_param', ['index':'2'])) WebUI.verifyEqual(content, 'Alternative Dispute Resolution') > ``` > > This test case runs successful. > >

    Why does this model not fit in obj by Xpath

    QQ图片20181001153226.png

    QQ图片20181001153353.png

    ‘By.xpath’ ? Why does it appear here? I do not understand.

    Please take the screenshot of the Test Object “Ojbect Repository/Page_/label_parameterized” and show it.

    kazurayam said:

    ‘By.xpath’ ? Why does it appear here? I do not understand.

    Please take the screenshot of the Test Object “Ojbect Repository/Page_/label_parameterized” and show it.

    I changed your way,Trying to assign variables in Xpath

    Please have a look at:
    https://docs.katalon.com/pages/viewpage.action?pageId=12419075

    This page is a bit outdated.

    Warning
    Parameterizing Web/Mobile Test Object Properties is only available with Basic Selection Method.

    As for version 5.7.0, this sentence should be rewritten:

    Warning
    Parameterizing Web/Mobile Test Object Properties is only available with Attribute Selection Method.

    Jester,

    Your test object has ‘XPath’ as selection method. This would not accept parameter ${index} ---- I suppose, I have not checked it myself, I may be wrong.

    Try changing the selection method from XPath to Attribute, and retry.

    kazurayam said:

    Please have a look at:
    https://docs.katalon.com/pages/viewpage.action?pageId=12419075

    This page is a bit outdated.

    Warning
    Parameterizing Web/Mobile Test Object Properties is only available with Basic Selection Method.

    As for version 5.7.0, this sentence should be rewritten:

    Warning
    Parameterizing Web/Mobile Test Object Properties is only available with Attribute Selection Method.

    Jester,

    Your test object has ‘XPath’ as selection method. This would not accept parameter ${index} ---- I suppose, I have not checked it myself, I may be wrong.

    Try changing the selection method from XPath to Attribute, and retry.

    yeah,ojb by Attribute way is ok,But sometimes I have to find obj by Xpath,and i have to copy a right xpath to the Attribute mesthod (like jpg), This’s too much trouble.it’s not cool

    thank you for your help!
    If you have a better way, please contact me.
    谢谢!

    Hi Im having problem about inspect for dropdown too. I tried to inspect dropdown on ODOO. but it’s hard because value of id always generate. Can you help?

    How to get value of dropdown? thanks