Find element by class name selenium
Selenium find element by class name used when there is class name available for element. Selenium support find by class name method to locate element by class name. Generally we are using id, name to find element. But if element do not have such identifiers and only class name is available then we can use find by class name.
Expression to find element by class name selenium is driver.findElement(By.className("class name of element"));
Let us see how to get class name of element.
How to find class name of element?
As you can see in above image, Link element have clickLink class. You can use that class name in your selenium test script to locate that link element. When you run test, selenium find element by class name.
Syntax to locate link element by class name is as below.
- WebElement eleLink= driver.findElement(By.className("clickLink"));
- eleLink.click();
Example of selenium find element by class name
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FindByClassName {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.navigate().to("http://only-testing-blog.blogspot.com/2013/10/table.html");
//find element by class name.
WebElement eleLink= driver.findElement(By.className("clickLink"));
eleLink.click();
driver.close();
}
}
No comments:
Post a Comment