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; }
Short but detailed explanation!! This post very clear and easy to understand.Thank you Very much!!
LikeLiked by 1 person
Thanks GOPIKA :))
LikeLike