There are many different ways of locating elements in webdriver for your software web application page and I have described one of them in my PREVIOUS POST with example. In webdriver, we can't do any thing on web page of software web application if you don't know how to locate an element. As you know, we can use element's ID to locate that specific element but suppose if your element do not have any ID then how will you locate that element on page of software application? Locating web element by className is good alternative if your element contains class name. We can locate element By Tag Name, By Name, By Link Text, By Partial Link Text, By CSS and By XPATH too but will look about them in my next posts.
Create selenium webdriver data driven framework from scratch @This Page.
Create selenium webdriver data driven framework from scratch @This Page.
How to get the class name of element
You can get the class name of element using firebug add-on software as shown in bellow given image.
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..
if there is a link on the page and we want verify the text of that link is it necessary to use link locator
ReplyDeleteGreat Work
ReplyDeleteGood article.
ReplyDeleteMay I ask what application do you use to view the object and the UI instantly at the same time?
ReplyDeleteOr 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...
Deletejasbir.singh@fatbit.com
What application do you use to view the source code and UI instantly at the same time?
ReplyDeleteOr 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.
Great
DeleteNever mind, I didn't notice the word FIREBUG...
ReplyDeleteThanks anyway...
These posts will help you
Deletehttp://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
Hi,
ReplyDeleteI got proper knowledge on selenium Web Driver by your tutorials.
Thank a lot
Hi Aravind,
ReplyDeletei am unable to launch u r demo site what is the reason for that?
There was some issue with URL. Please check now
Delete