How to select listitem values from list | Selenium Forum
M
Posted on 06/02/2016
package com.carwale;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class displaybrandlist {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://carwale.com");
driver.findElement(By.xpath(".//*[@id='globalcity-popup']/div[1]/div[1]")).click();
System.out.println("pop up closed");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='nav']/ul/li[3]/a/span[2]")).click();
System.out.println("new cars clicked");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='nav']/ul/li[3]/ul/li[1]/a")).click();
System.out.println("clicked on find cars");

driver.findElement(By.xpath("html/body/div[9]/section[1]/div[1]/div/div[1]/ul/li[1]"));
System.out.println("brandtab select");

Select brands =new Select(driver.findElement(By.xpath("html/body/div[9]/section[1]/div[1]/div/div[1]/ul/li[1]")));
List<WebElement> oSize = brands.getOptions();
int iListSize = oSize.size();
for(int i =0; i < iListSize ; i++){
// Storing the value of the option
String sValue = brands.getOptions().get(i).getText();
// Printing the stored value
System.out.println(sValue);


// TODO Auto-generated method stub

}
}
}



error:Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "li"

M
Replied on 07/02/2016

Hi Mrudula,

Could you please explain your scenario, for which items you want to make the list.

Thanks & Regards,
Mohit Kumar Mongia


M
Replied on 07/02/2016

I have modified your code to print the list of brands available on the page. Please check and let me know if you are looking for the same thing

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class carwale {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://carwale.com");
driver.findElement(By.xpath(".//*[@id='globalcity-popup']/div[1]/div[1]")).click();
System.out.println("pop up closed");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='nav']/ul/li[3]/a/span[2]")).click();
System.out.println("new cars clicked");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='nav']/ul/li[3]/ul/li[1]/a")).click();
System.out.println("clicked on find cars");

driver.findElement(By.xpath("html/body/div[9]/section[1]/div[1]/div/div[1]/ul/li[1]"));
System.out.println("brandtab select");

List<WebElement> brands = driver.findElements(By.xpath("//*[@id='brand-type']/div[1]/ul[1]/li"));
for (int i = 0; i < brands.size(); i++) {
System.out.println(brands.get(i).getText());
}




}
}


M
Replied on 07/02/2016

Thank you mohit . that works Responsive image