how to list the list the value the values in a dropdown in selenium webdriver

57 views
Skip to first unread message

Esakki Sankar

unread,
May 4, 2015, 8:04:06 PM5/4/15
to webd...@googlegroups.com
hi all,


Am new to selenium webdriver am learning just now ,
am sorry if i was a  very very basic question

I want to list all the items in my console form the popup

Here is my code

public class changeCurrency {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

                    

             

              WebDriver driver = new FirefoxDriver();

              driver.get("http://www.cleartrip.com/");

             

              driver.findElement(By.xpath("//a[contains(@class,'listMenuLink currencyLink')]")).click();

              List<WebElement> Currency_list = driver.findElements(By.xpath("//ul[contains(@class,'currencyCol')]"));

              for(int i = 0; i< Currency_list.size();i++)

              {

                           System.out.println(Currency_list.get(i).toString());

                                 

              }

      

 

My output was

[[[FirefoxDriver: firefox on WINDOWS (de810f41-6237-47aa-bbbe-f40c977bb4e2)] -> xpath: //ul[contains(@class,'currencyCol')]], [[FirefoxDriver: firefox on WINDOWS (de810f41-6237-47aa-bbbe-f40c977bb4e2)] -> xpath: //ul[contains(@class,'currencyCol')]]]

2

[[[FirefoxDriver: firefox on WINDOWS (de810f41-6237-47aa-bbbe-f40c977bb4e2)] -> xpath: //ul[contains(@class,'currencyCol')]], [[FirefoxDriver: firefox on WINDOWS (de810f41-6237-47aa-bbbe-f40c977bb4e2)] -> xpath: //ul[contains(@class,'currencyCol')]]]

2

 

But i want to list like

Australian dollar

Euro

Hongkong dollar

Indian rupee

Like that could u please advice on  this


sample image



Thanks in advance

darrell

unread,
May 9, 2015, 1:22:45 AM5/9/15
to webd...@googlegroups.com
First, the class attribute could equal "listMenuLink currencyLink" or it could be "currencyLink listMenuLink". Both are equally valid but your XPath will only match one of them. With xpath you REALLY want to use:

    By.xpath("//a[contains(@class, 'listMenuLink') and contains(@class, 'currencyLink')]")

However, element attributes are more geared towards CSS. So I would use:

    By.cssSelector("a.listMenuLink.currencyLink")

it is shorter, easier to read and probably faster than using XPath. So opening the list of currency would be:

    driver.findElement(By.cssSelector("a.listMenuLink.currencyLink")).click();

What you are actually trying to get is not in the UL element. In the one UL is a list of LI elements. You don't want everything in the LI however. You want the second SPAN inside the A which is inside the LI. So the CSS selector could be:

    By.cssSelector("ul.currencyCol>li>a>span.span17")

The nice thing about CSS is that you don't have to be that precise. This is saying you want to find the UL with class="currencyCol" then immediately under that you want to find all the LI elements. Under all the LI elements you want all the A elements and immediately under the A element will be a SPAN with class="span17". To simplify this I would use:

    By.cssSelector("ul.currencyCol .span17")

So the list will be:

    List<WebElement> currencyList = driver.findElements(By.cssSelector("ul.currencyCol .span17"));

and the for loop would be:

    for(int i = 0; i < currencyList.size(); i++) {
        System.out.println(currencyList.get(i).getText());
    }

But this can be simplified to:

    for(WebElement currency : currencyList) {
         System.out.println(currency.getText());
    }

Both these loops are functionally equivalent but the second one is more in line with current Java syntax. Also, don't forget:

    driver.quit();

at the end to close the browser and end the session.
...

Shakti Kumar Singh

unread,
May 13, 2015, 10:57:24 PM5/13/15
to webd...@googlegroups.com
Hi Esakki,

instead of using  

    Currency_list.get(i).toString()

use

   Currency_list.get(i).getText()


It will give you the desired output. 

Thanks

vasantha marimuthu

unread,
May 13, 2015, 10:57:24 PM5/13/15
to webd...@googlegroups.com
package BDD;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.List;

/**
* Created by PRAKASH on 5/8/2015.
*/
public class cleartrip {

@Test
public void test()
{
WebDriver dr=new FirefoxDriver();
dr.get("http://www.cleartrip.com/");
dr.findElement(By.xpath("//*[@id='userAccountNav']/nav/ul/li[2]/a")).click();
List<WebElement> currencies=dr.findElements(By.xpath("//*[@id='userAccountNav']/nav/ul/li[2]//li/a"));
for(WebElement a:currencies)
{
System.out.println(a.getText());
}
}
}
...
Reply all
Reply to author
Forward
0 new messages