Reputation: 1
For practice I was working on code and faced a very strange problem. I am trying to find the list of all elements using className. When I check the list size it returned me 0 but the same class when used with xpath works. My code is below
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.localbanya.com/home/search?searchKeyword=dove");
driver.findElement(By.xpath("//*[@class='ms-choice']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[@class='ms-drop bottom']//li[4]/label")).click();
driver.findElement(By.cssSelector(".proceed-button.button")).click();
Thread.sleep(1000);
List<WebElement> prList = driver.findElements(By.xpath("//*[@class='prName']"));
// This particular code does not work but same class name used above works
//----Interesting not working
//List<WebElement> prList = driver.findElements(By.className(".prName"));
System.out.println(prList.size());
for (WebElement web : prList)
{
System.out.println(web.getText());
}
//driver.close();
}
Upvotes: 0
Views: 123
Reputation: 3058
List<WebElement> prList = driver.findElements(By.className("prName"));
It will work, no need to use .
while you are using By.className
. .
is required when you use css selector.
Upvotes: 1