Search code examples
xmlhtmldomautomationanywhere

How to write the relative path expression (XPath) to extract the Card Details from the following screenshot?


This is the image from google chrome

Hi All,

I am trying to write the relative expression(XPath) to get the card details from the following website

https://www.fakenamegenerator.com/gen-random-us-us.php

I see that the card name keeps alternating between VISA and MasterCard,

I wanted to know if the following expression is possible

"//dl[dt/text()='MasterCard']/dd OR //dl[dt/text()='Visa']/dd"

Can I use OR operator to make the expression generic so that it will work for both the cases VISA or Master Card?

FYI this is for an Automation Anywhere project where I am practicing to enhance my skills in WEB Automations


Solution

  • You were close, but you cannot use the or operator like that. It is used when you want to have multiple predicate conditions. If you would like to have multiple possible node results (like in your example), you would use a pipe | operator

    1. //dl[dt/text()='MasterCard']/dd | //dl[dt/text()='Visa']/dd
    2. //dl[dt/text()='MasterCard' or dt/text()='Visa']/dd
    3. //dl[dt[text()='MasterCard' or text()='Visa']]/dd

    The first option says:
    Get me a dd element that is a direct child of a dl element with a dt element with text 'MasterCard'
    OR
    a dd element that is a direct child of a dl element with a dt element with text 'Visa'.

    The second option says:
    Get me a dd element that is a direct child of a dl element with a dt element with text 'MasterCard' OR 'Visa'.

    The third option is just a slightly more compact version of the second one.

    So while both options will give you the same results (in this case), the performance may be very different. Using the pipe operator basically means that you are doing multiple searches rather than a single complex one.