Difference between find
element () and find elements ()?
findElement() :
Find the first element
within the current page using the given "locating mechanism".
Returns a single
WebElement.
Syntax: WebElement
findElement(By by)
Ex:
driver.get("http://ruchi-myseleniumblog.blogspot.in/");
WebElement widget = driver.findElement(By .xpath(".//*[@id='BlogArchive1_ArchiveList']"));
widget.click();
findElements() :
Find all elements
within the current page using the given "locating mechanism".
Returns List of
WebElements.
Syntax:
WebElement ullist = driver.findElement(By.className("posts"));
WebElement ullist = driver.findElement(By.className("posts"));
List<WebElement> posts = ullist.findElements(By.tagName("li"));
System.out.println("List of Posts are Below");
for (int i = 0; i < posts.size(); i++) {
String post = posts.get(i).findElement(By.tagName("a")).getText();
System.out.println(post);
}
Sample Program:
package programs;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Sample {
WebDriver driver;
@Test
public void test() {
driver = new FirefoxDriver();
driver.get("http://ruchi-myseleniumblog.blogspot.in/");
WebElement widget = driver.findElement(By
.xpath(".//*[@id='BlogArchive1_ArchiveList']"));
WebElement hierarchy = widget.findElement(By.className("hierarchy"));
System.out.println("Year is "
+ hierarchy.findElement(By.className("post-count-link"))
.getText());
System.out.println("Year-Posts size is "
+ hierarchy.findElement(By.className("post-count")).getText());
WebElement sublist = hierarchy.findElement(By.className("hierarchy"));
System.out.println("Month is "
+ sublist.findElement(By.className("post-count-link"))
.getText());
System.out.println("Month-Posts size is "
+ sublist.findElement(By.className("post-count")).getText());
WebElement ullist = driver.findElement(By.className("posts"));
List<WebElement> posts = ullist.findElements(By.tagName("li"));
System.out.println("List of Posts are Below");
for (int i = 0; i < posts.size(); i++) {
String post = posts.get(i).findElement(By.tagName("a")).getText();
System.out.println(post);
}
driver.close();
}
}
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Sample {
WebDriver driver;
@Test
public void test() {
driver = new FirefoxDriver();
driver.get("http://ruchi-myseleniumblog.blogspot.in/");
WebElement widget = driver.findElement(By
.xpath(".//*[@id='BlogArchive1_ArchiveList']"));
WebElement hierarchy = widget.findElement(By.className("hierarchy"));
System.out.println("Year is "
+ hierarchy.findElement(By.className("post-count-link"))
.getText());
System.out.println("Year-Posts size is "
+ hierarchy.findElement(By.className("post-count")).getText());
WebElement sublist = hierarchy.findElement(By.className("hierarchy"));
System.out.println("Month is "
+ sublist.findElement(By.className("post-count-link"))
.getText());
System.out.println("Month-Posts size is "
+ sublist.findElement(By.className("post-count")).getText());
WebElement ullist = driver.findElement(By.className("posts"));
List<WebElement> posts = ullist.findElements(By.tagName("li"));
System.out.println("List of Posts are Below");
for (int i = 0; i < posts.size(); i++) {
String post = posts.get(i).findElement(By.tagName("a")).getText();
System.out.println(post);
}
driver.close();
}
}
If the locator is such that it always returns just one element, then the explanation you have cited out doesnt differentiate between findElement() and findElements() at all. And it would sound as if they are the same methods.
ReplyDeleteThe real difference between findElement() and findElements() is that findElement() throws a NoSuchElementException if the locator didnt match any elements, but findElements() doesnt throw any such exceptions but returns a List of size zero!
You might want to highlight that part.