findelement: used to identify a web element in the web page and return the first web element(one web element).
findelements: used to identify web elements in the web page and returns the list of web elements (more than one web element).
The term web element refers to the HTML controls on the web page, the HTML controls are
> Text Input.
> Checkboxes.
> Radio Box.
> Select Box.
> File Select.
> Hidden Controls.
> Clickable Buttons.
> Submit and Reset Button and so on
driver.findelement
will query the particular web page to find the specified web element in a DOM, if the element is present it will return the matching element otherwise it will throw an error.
Syntax for findElement:
driver.findElement(By.("<>")).click();
Example : driver.findElement(By.id("id1")).click();
package test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class SeleniumTest {
public static void main(String[] args) throws InterruptedException {
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
WebDriver driver = new ChromeDriver(options);
driver.get("https://qafeast.com/demo");
// To find the element
WebElement ele = driver.findElement(By.xpath("(//input[@id='editabletext'])[1]"));
// To click the element
ele.click();
// To click the element
driver.findElement(By.xpath("(//input[@id='editabletext'])[2]")).click();
driver.quit();
}
}
Syntax for findElements:
driver.findElements(By.("<>"));
Example : driver.findElements(By.xpath("//li/label"));
driver.findElements gives the List of elements.
We can query and get all the elements, some times we may need to get the text and verify or we may need to click the particular element.
Note: driver.findElements gives the count 0 if there are no elements matching with locators and it will not give any errors. It is always recommended to fail the script if the count is 0.
package test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.List;
public class SeleniumTest {
public static void main(String[] args) throws InterruptedException {
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
WebDriver driver = new ChromeDriver(options);
driver.get("https://qafeast.com/demo");
// Return all the elements
List els = driver.findElements(By.xpath("//li/label"));
// To print the number of elements
System.out.println("Total Elements are - "+els.size());
// To iterate each element and print the text and click the element
for(WebElement ele : els){
System.out.println("ele text is - "+ele.getText());
ele.click();
}
driver.quit();
}
}
Syntax for findElement: using Selenium Csharp
driver.FindElement(By.(“"));
Example: driver.FindElement(By.XPath("(//input[@id='editabletext'])[1]"));
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace ConsoleApp1
{
internal class SeleniumTest
{
static IWebDriver driver;
static void Main(string[] args)
{
driver = new ChromeDriver(@"\\Driver\\chromedriver.exe");
driver.Navigate().GoToUrl("https://qafeast.com/demo");
// To find the element
IWebElement ele = driver.FindElement(By.XPath("(//input[@id='editabletext'])[1]"));
// To click the element
ele.Click();
// To click the element
driver.FindElement(By.XPath("(//input[@id='editabletext'])[2]")).Click();
driver.Quit();
}
}
}
Syntax for findElements: Using Selenium Cshap
driver.FindElements(By.(“”));
Example : driver.FindElements(By.XPath("//li//label"));
We can query and get all the elements, some times we may need to get the text and verify or we may need to click the particular element.
Note: driver.findElements gives the count 0 if there are no elements matching with locators and it will not give any errors. It is always recommended to fail the script if the count is 0.
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace ConsoleApp1
{
internal class SeleniumTest
{
static IWebDriver driver;
static void Main(string[] args)
{
driver = new ChromeDriver(@"\\Driver\\chromedriver.exe");
driver.Navigate().GoToUrl("https://qafeast.com/demo");
// To find the elements
var eleList = driver.FindElements(By.XPath("//li//label"));
string tabName = "";
foreach(var ele in eleList)
{
tabName = ele.Text;
Console.WriteLine(tabName);
if (tabName.Equals("Button")) {
ele.Click();
Console.WriteLine("Button tab is clicked");
}
}
driver.Quit();
}
}
}