Search code examples
seleniumgettextgetvalue

How can I get a value of a specific item using XPath?


I want to compare the Outstanding balance amount with the database amount. How do I extract/get the amount to perform an assertion? Here I have given the screenshot of the outstanding amount. Using XPath how to get the amount 11,350.53?

enter image description here


Solution

  • You need to use the .text attribute of the WebElement returned by the XPath to get the string $ 11,350.53:

    In Python:

    text = driver.find_element_by_xpath("//label[text()='Outstanding Balance:']/following-sibling::div/p").text
    print(text.strip())
    

    In Java:

    string text = driver.findElement(By.xpath("//label[text()='Outstanding Balance:']/following-sibling::div/p")).getText();
    System.out.println(text.trim());
    

    The above XPath begins the query on the label element containing text Outstanding Balance:. The label element's following-sibling is <div class='col-xs-5 text-right'>. After locating this div, we query on its child element p which contains the text $ 11,350.53.