[Selenium] Ways to inspect Web elements

There many ways to locate a web element:

  • by name

<input name="email" />

  • by id

<input id="email"/>

  • by class

<button class="btn_on" />

  • by css
  • by xpath
  • by link text
<div href="/help.html">Help</div>

Then i created a method to handle all case above like:

public static By getElement(String how,String locator){

By el = null;

if (how.equalsIgnoreCase("name")) el = By.name(locator);

elseif(how.equalsIgnoreCase("id")) el = By.id(locator);

elseif(how.equalsIgnoreCase("xpath")) el = By.xpath(locator);

elseif(how.equalsIgnoreCase("linktext")) el = By.linkText(locator);

else System.out.println("Cannot locate element with "+how);

return el;



3 thoughts on “[Selenium] Ways to inspect Web elements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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