Kalyani Kalyani - 5 months ago 27
Java Question

can not retrive value from xpath for symbol but when it is int then it work correctly

I am using selenium webdriver (JAVA)test web application.i would like test the weather the utilities are available on that country or not for availability/unavailability used symbol cross and tick. i was tried all the combinations of xpath but it print blank value below is related html

<span ng-reflect-class-name="glyphicon glyphicon-remove" class="glyphicon glyphicon-remove"></span>
<img alt="" data-placement="top" data-toggle="tooltip" ng-reflect-src="app/shared/images/surro_icon1.png" src="app/shared/images/surro_icon1.png" ng-reflect-title="SAFE WATER: Not Available" title="SAFE WATER: Not Available">

and related code for that :Actions builder = new Actions(driver);

WebElement SafeWater = driver.findElementByXPath("/html/body/app-root/app-home/div/div/div[1]/app-utility-bar/div/ul/li[3]/span");
String availabilitySafeWater = SafeWater.getText();
System.out.println( "safe water :" + availabilitySafeWater);

Guy Guy

The <span> tag doesn't have text, that is why you get blank values. I think you are looking for the title of the <img> tag

WebElement safeWater = driver.findElement(By.xpath("/html/body/app-root/app-home/div/div/div[1]/app-utility-bar/div/ul/li[3]/img"));
String availabilitySafeWater = safeWater.getAttribute("title");
System.out.println( "safe water :" + availabilitySafeWater);

Result: safe water : SAFE WATER: Not Available

As a side note, try not to use absolute path. For example, if glyphicon is unique class name you can do

WebElement safeWater = driver.findElement(By.xpath(".//[@class='glyphicon']"));

Or even using By.className

WebElement safeWater = driver.findElement(By.className("glyphicon"));