[Selenium WebDriver Videos]: How to find a element which doesnt exist i.e findElements

Опубликовано: 25 Октябрь 2024
на канале: Udaya Kumar Anem
15,042
25

Hi in this video i demonstrated about how to find WebElements which may or may not exists.
fineElements with collection size greater than 0
fineElements with collection size 0.

Please find the below code which i used during my demonstration:

public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
/*driver.get("http://www.google.com");
List<WebElement> links = driver.findElements(By.tagName("a"));
int noButtons=links.size();
System.out.println("No. of buttons in the web page "+noButtons);
if(noButtons>0) 'Greater than 0
{
for(WebElement link:links)
System.out.println(link.getText());
}*/

driver.get("file:///E:/Programming%20Samples/HTML%20Samples/findElementLogin.htm");
driver.findElement(By.id("uname")).sendKeys("xxx");
driver.findElement(By.id("pwd")).sendKeys("yyy");
driver.findElement(By.id("Login")).click();

WebElement welcome=driver.findElement(By.id("label1"));
if(welcome.isDisplayed())
System.out.println("Welcome message displayed");

/*WebElement welcome1=driver.findElement(By.id("label2"));
if(!welcome1.isDisplayed())
System.out.println("My test is passed");*/

List<WebElement> labels=driver.findElements(By.id("label2"));
int noLabels=labels.size();
if(noLabels==0)
System.out.println("My test is passed");

driver.quit();
}