Finding WebElements

We have the option to find a single WebElements or create a list of multiple elements.

Find a single WebElement

<code lang="java">
package book.chapter05;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import book.chapter09.tests.DriverBase;

public class FindSingleElement extends DriverBase {
  
  @BeforeMethod
  public void setUp() {
    driver.get("http://techblog.polteq.com/testshop/index.php?controller=contact");
  }
  
  @Test
  public void findElement() {
    WebElement emailTextfield = driver.findElement(By.id("email"));
  }

}
</code>

Find multiple WebElemets

<code lang="java">
package book.chapter05;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import book.chapter09.tests.DriverBase;

public class FindMultipleElements extends DriverBase {
  
  @BeforeMethod
  public void setUp() {
    driver.get("http://techblog.polteq.com/testshop/");
  }
  
  @Test
  public void findElement() {
    List<WebElement> tags = driver.findElements(By.cssSelector("#tags_block_left p a"));
  }

}
</code>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.