2

Kindly find attached the html code of the section in question enter image description here

I use the following code and managed to get the text for Status

successMessage = driver.findElement(By.xpath("//span[@class='status-success' and @data-v-5d062b04 ='']")).getText();

However, I need help to get the number of the Transaction Number field (ie, the 12267A.......) . I can get the successMessage because there is a specific class for it, called class = "status-success". There is no such specific identifier for the transaction Number which I can use to extract the numerical text.

Hope to havce advice on how to extract the actual trans number.

1 Answer 1

3

Based on the information you shared, I was able to get the Transaction Number with this line of code:

driver.findElement(By.xpath("//span[@class='status-success' and @data-v-5d062b04 ='']//ancestor::body/div/div/span[@data-v-5d062b04 =''][1]")).getText()

Referring your screenshot I tried to recreate the HTML:

<div data-v-5d062b04 class="row my-2" style></div>
    <div data-v-5d062b04 class="row my-2" style>
        <div data-v-5d062b04 class="card-item-title col-sm-4">" Transaction Number "
            <!---->
        </div>
        <div data-v-5d062b04 class="col-sm-8">
            <span data-v-5d062b04>12267A2CCM1X4BE9FA5E3</span>
        </div>
    </div>
    <div data-v-5d062b04 class="row my-2" style>
        <div data-v-5d062b04 class="card-item-title col-sm-4">" User Information "
            <!---->
        </div>
        <div data-v-5d062b04 class="col-sm-8">
            <span data-v-5d062b04> 778478932 (QAColombia#3335) </span>
        </div>
    </div>
    <div data-v-5d062b04 class="row my-2" style>
        <div data-v-5d062b04 class="card-item-title col-sm-4">" Status "
            <!---->
        </div>
        <div data-v-5d062b04 class="col-sm-8">
            <span data-v-5d062b04 class="status-success"> Successful </span>
        </div>
    </div>

And then used this Selenium + java code to get the data:

String successMessage = driver.findElement(By.xpath("//span[@class='status-success' and @data-v-5d062b04 ='']")).getText();
if(successMessage.contains("Successful")) {
    System.out.println(driver.findElement(By.xpath("//span[@class='status-success' and @data-v-5d062b04 ='']//ancestor::body/div/div/span[@data-v-5d062b04 =''][1]")).getText());
}
7
  • Thanks again for your help. Can you advise me, if using @class = 'status-success' wouldn't that just point to the element in "Status" only ? How does it relate to Transaction Number ? Thank you for your guidance Jun 17, 2022 at 5:57
  • Oh, i think i see it. It points to the grouping where it consist of SuccessMessage, and then find the Trans Number within the group ? Jun 17, 2022 at 5:59
  • 1
    What I did is, find the success element, then found a common ancestor of that element. then I traversed to the transaction number. So hope this helped you. Jun 17, 2022 at 6:03
  • 1
    By the way, does the '1' in "ancestor::body/div/div/span[@data-v-5d062b04 =''][1]"))." refers to the 1st digit of the transaction number ? If that is the case, what if the transaction number does not necessarily start with "1" ? It could be a random number number of alphabet because it's a unique ID and it will be randomly generated. Really appreciate your advice Jun 17, 2022 at 6:24
  • 1
    The [1] is the index of the element. In your HTML there are more than one <span> with similar attributes. So the [1] gets the first appearance of the <span> with matching conditions. Jun 17, 2022 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.