cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
twinkywinky
Frequent Visitor

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

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.

4 ACCEPTED SOLUTIONS

Accepted Solutions

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.

 

View solution in original post

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.

View solution in original post

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

 

View solution in original post

VJR
Super User
Super User

Hi @twinkywinky 

Not on the List but this is done directly in the javascript itself.

 

Starting from 0 Banana is at 4th position and is a duplicate.

 

VJR_0-1640251539221.png

 

 

function ExecuteScript(inputstring = %CountryNameVariableHere%) 
{	
  var foundflag;
  var x = document.getElementById("country");
  var txt = "";
  var txtDupes = "";
  var i;
  var myindex;
  foundflag=0;
  
  for (i = 0; i < x.length; i++) 
  {
  
  		if(x.options[i].text == inputstring)
      {
      
      	if(foundflag==1)
      		{	        	
          	return(myindex);      
      		} 
          else
          {
          	foundflag=1;        
        		myindex=i;
          }
      }       
   }    

  }

 

Notice in the first line you need to pass your country variable

 

function ExecuteScript(inputstring = %CountryNameVariableHere%)

View solution in original post

14 REPLIES 14
VJR
Super User
Super User

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
Frequent Visitor

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.

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&colon; $'''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
Frequent Visitor

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!

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
Frequent Visitor

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!

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
Frequent Visitor

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.

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
Frequent Visitor

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.

Hi @twinkywinky 

 

When you change 

This line

txtDupes = txtDupes + x.options[i].text + ";";

To this:

txtDupes = txtDupes + x.options[i].text + "*" + i + ";";

 

It will add the index value of that duplicate item separated by an Asterisk * character.

 

So in PAD when you split the "Orange*2" by the * character, then you will get the index 2.

 

VJR_0-1640233425458.png

 

 

Good luck.

twinkywinky
Frequent Visitor

Hi @VJR 

 

Can I get the index of a value in the list by searching in the list instead of having the index behind the values? 

 

Eg. %fruit% = banana

 

List

Apple

Orange

Orange

Pineapple

Banana

Banana

 

So the code will return Index = 5, when I search for %fruit% in the list.

 

Sorry for the trouble, thank you for all the help you have provided me with so far.

VJR
Super User
Super User

Hi @twinkywinky 

Not on the List but this is done directly in the javascript itself.

 

Starting from 0 Banana is at 4th position and is a duplicate.

 

VJR_0-1640251539221.png

 

 

function ExecuteScript(inputstring = %CountryNameVariableHere%) 
{	
  var foundflag;
  var x = document.getElementById("country");
  var txt = "";
  var txtDupes = "";
  var i;
  var myindex;
  foundflag=0;
  
  for (i = 0; i < x.length; i++) 
  {
  
  		if(x.options[i].text == inputstring)
      {
      
      	if(foundflag==1)
      		{	        	
          	return(myindex);      
      		} 
          else
          {
          	foundflag=1;        
        		myindex=i;
          }
      }       
   }    

  }

 

Notice in the first line you need to pass your country variable

 

function ExecuteScript(inputstring = %CountryNameVariableHere%)

twinkywinky
Frequent Visitor

Hi @VJR 

 

Nice it works! I really appreciate your help so much, thank you and have nice day!

Helpful resources

Announcements
 WHAT’S NEXT AT MICROSOFT IGNITE 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Register for a Free Workshop.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

September Events 2022

Check out all of these events

Attend in person or online, there are incredible conferences and events happening all throughout the month of September.

Top Solution Authors
Top Kudoed Authors
Users online (2,932)