-5
<div class="tax_details">
    <ul class="clearfix">
         <li>
            <p>Total Income</p>
            <span>£33,254.00</span>
         </li>
         <li class="text-center">
            <p>Taxable Income</p>
            <span>£21,054.00</span>
         </li>
         <li class="text-right">
            <p>Tax Due</p>
            <span>£4,210.80</span>
         </li>
     </ul>
 </div>

How to extract Totalincome and Tax Due , Taxable income without id in selenium using findby

joe
  • 34,529
  • 29
  • 100
  • 137
  • This is a bit similar to [this question](https://sqa.stackexchange.com/questions/28716/find-xpath-for-dynamic-numeric-value-in-a-web-page/28802#28802) from the software quality assurance and testing exchange site. – mrfreester Aug 21 '17 at 19:52

4 Answers4

2

Using css, you could do it this way given that the total income doesn't have a unique way to get the element:

List<WebElement> listItems = driver.findElements(By.cssSelector("li > span"));
listItems.get(0).getText(); //Total Income
listItems.get(1).getText(); //Taxable Income
listItems.get(2).getText(); //Tax Due
smit9234
  • 361
  • 1
  • 7
1

You could do use FindElements() to get all the elements then loop through them locally so you don't have to keep hitting the site. Although with only 2 elements it may not be a big deal but nonetheless if in the future the list grows you would already have it.

The following is untested psuedocode but should give you a pretty good idea what I mean:

List<WebElement> elements = driver.findElements(By.Xpath("//div[@class = 'tax_details']/ul");
for(WebElement element : elements) {
    WebElement item = element.findBy(By.Xpath("'./li');
    ... 
    ...
    ...
}

The idea is that you'll have all the elements in a list and then you could access the element attributes or build a switch statement or whatever your requirements are.

so cal cheesehead
  • 2,515
  • 4
  • 29
  • 50
1

Assuming you are asking to target the elements using the visual text clues, you can use XPath, something like:

WebElement taxDetails = driver.findElement(By.className("tax_details"));
WebElement totalIncome = taxDetails.findElement(By.xpath(".//li[contains(., 'Total Income')]"));
String totalIncomeAmount = totalIncome.findElement(By.tagName("span")).getText();
  1. First line retrieves a refecence to the entire block, assuming attribute class="tax_details" appears only once on the page.
  2. Second line retrieves the line li, which contains the text of interest. The leading dot in the XPath expression is vitally important in this case; see the official documentation.
  3. Last line retrieves the amount, which is located in the span element of the line of interest.

In real production, you will probably want to extract this into something more usable, perhaps a method that takes the "text of interest" as input.

SiKing
  • 10,003
  • 10
  • 39
  • 90
1

You can also use a Pure xpath statement. You will need three one each for eg. "Total Income"

/div/[@class"tax_details"]/ul[@class="clearfix"]/li[p/text()="Total Income"]/span/text()

to explain the statement

[p/text()="Total Income"] 

causes xpath to limit the returned li elements based on the text of sibling element resulting in only one span node being returned.

dank8
  • 361
  • 4
  • 20