1

I have a dropdown with some countries as follows,

enter image description here

I want to send the values US,CA,AF,AL,DZ,AS,AD to an array, loop it and print by using Selenium and Java.

I tried the following

WebElement elementdrop = d.findElement(By.xpath("path"));
List<WebElement> dropdownvalues = d.findElements(By.xpath("path"));
for(WebElement value:dropdownvalues)
{ 
  String pcvalues=value.getText();
  System.out.println("value names" + pcvalues);
 }

This will print United States Canada Afghanistan Albania etc. But I want like US CA AF AL DZ AS AD

4
  • Could you share the what have you done for the same,issues you were facing? Its pretty straight forward question!! Commented Jul 25, 2016 at 16:52
  • The above drop down contain the list of countries. In this list each country name have corresponding id like US,CA, AF etc. I want this ids to an array. Commented Jul 25, 2016 at 17:07
  • Have you tried anything? Could you share your code and issues you faced? Commented Jul 25, 2016 at 17:12
  • I tried like this, WebElement elementdrop = d.findElement(By.xpath("path")); List<WebElement> dropdownvalues = d.findElements(By.xpath("path")); for(WebElement value:dropdownvalues) { String pcvalues=value.getText(); System.out.println("value names" + pcvalues); } This will print United States Canada Afghanistan Albania etc. But I want like US CA AF AL DZ AS AD Commented Jul 25, 2016 at 17:17

1 Answer 1

3
 WebElement dropdown = driver.findElement(By.name("Country"));

 List<WebElement> options = dropdown.findElements(By.tagName("option"));

 Iterator<WebElement> it=options.iterator();

   while(it.hasNext())
    {
      System.out.println(it.next().getAttribute("Value"));
    }

Give this a try and let me know if it works.

Sign up to request clarification or add additional context in comments.

5 Comments

Also I need to do some thing like this. I have the country code stored in one variable like String CODE = "CA"; then, I need to check whether the CODE is available in dropdown. If it available , print System.out.println("Country available to choose"); else print System.out.println("Country not available to choose"); Could you please modify this code for this issue?
String CODE = "CA"; WebElement dropdown = driver.findElement(By.name("Country")); List<WebElement> options = dropdown.findElements(By.tagName("option")); Iterator<WebElement> it=options.iterator(); while(it.hasNext()) { if(it.next().getAttribute("Value")==CODE){ print System.out.println("Country available to choose"); } else { print System.out.println("Country not available to choose"); } }
Yeah you can do that whats the issue you are facing?
I have list of country codes. I want to check whether these country codes are matching with the drop down value.
You might have to brush up on coding skills. In this scenario as you have the value you should be able to use it for your calculation. Looking at your code what i see is you could use 'equalsIgnoreCase' instead of== that should resolve your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.