Create selenium webdriver data driven framework from scratch @This Page.
Look in to above image. Post date content has a class and we can use that class name to store that blog post date string. Notice one more thing in above image, blog post date string has not any ID so we can not locate it by ID.
(Note : You can view more webdriver tutorials on THIS LINK.)
Example Of Locating Web Element By ClassName
We can use bellow given syntax to locate that element of software web application.
driver.findElement(By.className(“date-header”));
Practical example of locating web element by className is as bellow.
package junitreportpackage;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Mytest1 {
WebDriver driver = null;
@Before
public void beforetest() {
// set geckodriver path.
System.setProperty("webdriver.gecko.driver", "D:\Selenium Files\geckodriver.exe");
//To open Firefox browser
driver = new FirefoxDriver();
//To Maximize Browser Window
driver.manage().window().maximize();
//To Open URL In browser
driver.get("http://only-testing-blog.blogspot.com/2013/11/new-test.html");
}
@After
public void aftertest() {
driver.quit();
}
@Test
public void test()
{
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
String datentime = driver.findElement(By.className("date-header")).getText();//Locating element by className and store its text to variable datentime.
System.out.print(datentime);
}
}
In above example,
Open Browser
WebDriver driver = new FirefoxDriver() will open webdriver’s Firefox browser instance.
Open URL
driver.get(); will open targeted URL in browser.
Maximize Browser Window
driver.manage().window().maximize(); will maximize browser window.
View THIS POST to know how to run webdriver test in google chrome and View THIS POST to know how to run webdriver test in IE browser..
12 thoughts on “Locating Web Element By ClassName In Selenium WebDriver with example”
if there is a link on the page and we want verify the text of that link is it necessary to use link locator
Great Work
Good article.
May I ask what application do you use to view the object and the UI instantly at the same time?
Or do you know something one which is open source?
I think this would be a big help to those who use selenium web driver.
Thanks a lot for your tutorials…
What application do you use to view the source code and UI instantly at the same time?
Or do you know an open source application to do this?
I think this will be a great help to locate any related IDs, Name etc in a particular object.
Thanks a lot for the tutorial.
Never mind, I didn't notice the word FIREBUG…
Thanks anyway…
These posts will help you
http://software-testing-tutorials-automation.blogspot.in/2015/07/steps-to-install-firebug-and-firepath.html
http://software-testing-tutorials-automation.blogspot.in/2015/03/install-webdriver-element-locator-add.html
Great
jasbir.singh@fatbit.com
Hi,
I got proper knowledge on selenium Web Driver by your tutorials.
Thank a lot
Hi Aravind,
i am unable to launch u r demo site what is the reason for that?
There was some issue with URL. Please check now