1

I have a page with a list of cards with information. The XPATHS of each cards are:

self.automatic_payments_cards_list = (By.XPATH , '//*[@id="page-inner"]/div/div/main/lseg-gateway-billing-payment-line-info')

I'm trying to get the text of a specific elements for every card in the page.

//*[@id="page-inner"]/div/div/main/lseg-gateway-billing-payment-line-info[1]/lseg-card/div/lseg-card-container/ng-transclude/div/div[4]/div/div[3]

//*[@id="page-inner"]/div/div/main/lseg-gateway-billing-payment-line-info[2]/lseg-card/div/lseg-card-container/ng-transclude/div/div[4]/div/div[3]

//*[@id="page-inner"]/div/div/main/lseg-gateway-billing-payment-line-info[3]/lseg-card/div/lseg-card-container/ng-transclude/div/div[4]/div/div[3]

I know that with this code i get all the text on each card

for i in range(len(self.driver.find_elements(*self.automatic_payments_cards_list))):
            print(self.driver.find_element(*self.automatic_payments_cards_list)[i].text)

But i don't want to get all the text on the cards, only the text on this specifics XPATHS

//*[@id="page-inner"]/div/div/main/lseg-gateway-billing-payment-line-info[**X**]**/lseg-card/div/lseg-card-container/ng-transclude/div/div[4]/div/div[3]**

Can you guys guide me in finding a solution to this?

2 Answers 2

1

The best way to actually achieve this is by using find element_by_xpath on the element.

all_card_els = driver.find_elements_by_xpath('//*[@id="page-inner"]/div/div/main/lseg-gateway-billing-payment-line-info')

for card_el in all_card_els:
    specific_el_within_card = card_el.find_element_by_xpath('.//lseg-card/div/lseg-card-container/ng-transclude/div/div[4]/div/div[3]')

The . at the starting of the xpath is essential to make sure that the search is within the selected el, without the . you will always end up getting the first el which matches this xpath on the page. You can now use specific_el_within_card however way you like inside the loop, or append it to an external list.

PS: You can access the text via specific_el_within_card.text() as you mentioned you wanted to extract info for each card.

Sign up to request clarification or add additional context in comments.

Comments

1

It is simple.:-) Transfer your xpath as dynamical string and pass it, like you do in a loop for e.g.

 parent_locator_String=string1+ iterator + string2
"//*[@id="page-inner"]/div/div/main/lseg-gateway-billing-payment-line-info["+i+"]/lseg-card/div/lseg-card-container/ng-transclude/div/div[4]/div/div[3]"

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.