web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / How to get non-unique ...
Power Automate
Answered

How to get non-unique items in drop down list using Javascript or Python?

(0) ShareShare
ReportReport
Posted on by 27

Hi,

 

How do I extract all the values in a drop-down list from a website and then proceed to filter out the non-unique items in the list of values? Thank you in advance.

I have the same question (0)
  • VJR Profile Picture
    7,635 on at

    Hi @twinkywinky 

     

    - Javascript to read all items from select

    http://jsfiddle.net/WfBRr/5/

    Use the "Run javascript function on webpage" to inject the above shown function.

     

    - Filter out the non-unique items -

    did you mean remove duplicates?

    Above code shows the list in an alert box.

    Read each of those values and insert them into a List variable using "Add item to list".

    Then use "remove duplicate items from list" action of PAD.

     

     

  • twinkywinky Profile Picture
    27 on at

    Hi @VJR 

     

    - Javascript to read all items in drop down list

    I am hoping to use any coding to extract all the values in the drop down list.

     

    This is my input

    twinkywinky_2-1639991002562.png

     

    This is the result I got for the result in a message box

    twinkywinky_1-1639990537484.png

     

    This is the website that I am trying with https://www.cssscript.com/demo/generic-country-state-dropdown-list-countries-js/ .

     

    - Filter out non-unique items

    Actually I am trying to keep the duplicates in the list instead of removing them. I tried looping through using For Each and it takes too long so looping through might not work for me. Thus, I though maybe python would be solution.

     

    Sorry and thank you in advance as I do not have much knowledge in coding.

  • Verified answer
    VJR Profile Picture
    7,635 on at

    Hi @twinkywinky 

     

    This is a working code for your scenario.

     

    • This is how the entire flow looks like

    I have added comments in the code for your understanding. Please go through them.

     

    VJR_0-1640001781629.png

     

     

    • Copy-paste the below code into a blank flow editor.
    WebAutomation.LaunchChrome Url: $'''https://www.cssscript.com/demo/generic-country-state-dropdown-list-countries-js/''' WindowState: WebAutomation.BrowserWindowState.Normal ClearCache: False ClearCookies: False Timeout: 60 BrowserInstance=> Browser2
    WebAutomation.ExecuteJavascript BrowserInstance: Browser2 Javascript: $'''function ExecuteScript() 
    {
     var x = document.getElementById(\"country\");
     var txt = \"\";
     var i;
     for (i = 0; i < x.length; i++) {
     txt = txt + x.options[i].text + \";\";
     }
    return(txt);
    }''' Result=> Result
    # Replace the words "Select Country;" with an empty string to remove it.
    Text.Replace Text: Result TextToFind: $'''Select Country;''' IsRegEx: False IgnoreCase: False ReplaceWith: $'''%''%''' ActivateEscapeSequences: False Result=> Replaced
    # Split the string into a List based on semicolon ; as a delimiter.
    Text.SplitWithDelimiter Text: Replaced CustomDelimiter: $''';''' IsRegEx: False Result=> TextList
    # Since the last item in the List is a blank row, remove it.
    Variables.RemoveItemFromListByValue Item: $'''%''%''' ItemMatchAllOccurrences: False List: TextList NewList=> TextList

     

    • The output after running this flow shows the list of 251 countries in a List variable

             

    VJR_1-1640001949769.png

     

     

    • I did not understand the second part of your post about duplicate and non-unique values.

     

  • twinkywinky Profile Picture
    27 on at

    Hi @VJR 

     

    First Part

    I tried it out and it works! Thank you very much, really appreciate it!

     

    Second Part

    I am trying to identify the duplicate values from a list and put these duplicate values into another list. You can see an example below, I need to get the second list from the first list.

    twinkywinky_0-1640141485932.png

    twinkywinky_1-1640141603296.png

     

    Third Part

    How do I find the index of a value in a list without using for each activity?

     

     

    Thank you for the help!

  • Verified answer
    VJR Profile Picture
    7,635 on at

    Hi @twinkywinky 

     

    Second part:

     

     

    function ExecuteScript() 
    {
     var x = document.getElementById("country");
     var txt = "";
     var txtDupes = "";
     var i;
     for (i = 0; i < x.length; i++) 
     {
    	if (txt.includes(x.options[i].text))
     { 
     if (txtDupes.includes(x.options[i].text)) 
     { 	
     //do nothing
     }
     else
     {
     txtDupes = txtDupes + x.options[i].text + ";";
     } 
     } 	
    
     txt = txt + x.options[i].text + ";";
     }
    return(txtDupes);
    }

     

     

    Above function will return the duplicates only once.

     

    Apple, Orange and Banana are more than once in the list and refer top display box after running the function only shows them once.

    I hope this is what you need.

     

    VJR_1-1640145037230.png

     

    As a side note, never seen a website having duplicate values in a dropdown. So why are you trying to achieve this?  

     

    I haven't got a chance to take a look at the Third part.

  • twinkywinky Profile Picture
    27 on at

    Hi @VJR 

     

    I am working with an internal site and there are duplicates in the dropdown and the different duplicates will actually have a different description each after selecting them. Thus I am trying to find out which are the duplicates and select them accordingly.

     

    Would be great if you could help me with the Third part as well. 

     

    Really grateful for your help!

  • VJR Profile Picture
    7,635 on at

    Hello @twinkywinky 

     

    Can you elaborate a bit more on the third part.

    Do you need the index of the dropdown value that is selected by the user on the webpage?

    Or do you need the index after your get the dropdown items into the PAD list variable?

    For what purpose do you need the index?

    And what do you do after you get the index?

     

    ~VJ

     

     

  • twinkywinky Profile Picture
    27 on at

    Hello @VJR 

     

    I need the index after getting the dropdown items into the PAD list variable so that I am able to select the specific item between the duplicates of it. I require the index as the automation would be able to select between the duplicates using value. After getting the index, the automation would then select it in the dropdown.

     

    I would only need to get the index of the first duplicate for the item and then I would add it with another index (eg the second apple in the 5 apple,) select the duplicate I want. Thus, I only need to find the index of the first duplicate of the item in the whole list variable.

     

    Not sure if my explanation was confusing, but thanks in advance.

  • Verified answer
    VJR Profile Picture
    7,635 on at

    Hi @twinkywinky 

     

    Since you do not want to use loops Excel will be the best bet.

     

    - Once you get the list from the website write it to Excel using "Write to Excel worksheet"

     

    - In this temporary Excel, keep a formula always written (one time activity). Also you can write the formula via PAD if you do not want to store it priorly. In this example D2 has the formula.

    and the formula is =SMALL(IF(ISNUMBER(SEARCH(C2,A:A)),ROW(A:A)),1)

    (After writing above formula in formula bar Press Control Shift Enter because this is an array formula in which case it adds a curly brace { and } to the start and end as seen below).

    - Using PAD write the Country of which you want to get the index of in cell C2. As soon as you write in C2, D2 will automatically calculate the index and give you the row number of the first duplicate.

    In this case first duplicate of Austria is in row number 14.

     

    - Read value of this single cell D2 in PAD via "Read from Excel Worksheet"

     

    - Since List variables start from 0 you need to do a minus 1 after getting value from excel.

    This would mean that your original List variable that you wrote into Excel will have Austria at row number 13.

     

    VJR_0-1640230616167.png

     

  • twinkywinky Profile Picture
    27 on at

    Hi @VJR 

     

    Thank you for the solution, but would it be possible to use Java or Python instead as I am trying to not open any applications.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 503 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 321 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard