Learn the difference between findElement() and findElements() in Selenium WebDriver using Java. These methods are commonly asked in Selenium interviews.
Example:
package TestAutomationCentral;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
import java.util.List;
public class WebElementDemo {
public static void main(String[] args) {
//TestAutomationCentral.com
System.setProperty("webdriver.chrome.driver", "C:\\temp\\drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts()
.implicitlyWait(Duration.ofSeconds(10));
driver.get("https://www.google.com/");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("TestAutomationCentral.com");
element.submit();
List<WebElement> elements =
driver.findElements(By.partialLinkText("Test Automation Central"));
for (WebElement e : elements) {
System.out.println(e.getText());
}
driver.quit();
}
}