I want to show in Extent Report the name of the web element ("USER_LOGIN" in my example); how do I get it?
I tried to use the getText()
but it replies with a blank as result.
For example this is site code:
<input class="form_control w100p ffg big" type="text"
name="USER_LOGIN" value="lpelet2@gmail.com" maxlength="255" placeholder="E-mail">
and this is what I'm trying to do:
public void verifyElementExist(WebElement weElem) throws ParserConfigurationException, SAXException, IOException
{
try
{
weElem.isDisplayed();
extnTest.log(LogStatus.INFO, "Element exist - " + weElem.getText());
}
catch (Exception e)
{
...
}
}
You can get details about difference between getText
and getAttribute
here.
Code below will get name or id:
String nameOrId = weElem.getAttribute("name") == null ? weElem.getAttribute("name") : weElem.getAttribute("id");
if (name == null)
name = "uknown";
Not all elements will have id or name, some elements you'll find using xpath or css selectors and code above will not do job for you. Easy way is to modify you method like below:
public void verifyElementExist(String name, WebElement weElem) throws ParserConfigurationException, SAXException, IOException
{
try
{
weElem.isDisplayed();
extnTest.log(LogStatus.INFO, "Element exist - " + name);
}
catch (Exception e)
{
...
}
}