Acessing multiple anchor elements inside text

Advertisements

I have the following XPATH

//a[@class='product-cardstyles__Link-sc-1uwpde0-9 bSQmwP hyperlinkstyles__Link-j02w35-0 coaZwR']

This xpath, finds a lot of anchor tags similar to the following HTML sample

<a href="/produto/10669/acucar-refinado-da-barra-pacote-1kg" class="product-cardstyles__Link-sc-1uwpde0-9 bSQmwP hyperlinkstyles__Link-j02w35-0 coaZwR" font-size="16" font-family="primaryMedium">Açúcar Refinado DA BARRA Pacote 1kg</a>

What I want to do is not to acess it is href, it is to grab the following string inside the anchor elements?

Açúcar Refinado DA BARRA Pacote 1kg

Sample code of what I am currently doing

    elements_list = EC.presence_of_all_elements_located((By.XPATH,"//a[@class='product-cardstyles__Link-sc-1uwpde0-9 bSQmwP hyperlinkstyles__Link-j02w35-0 coaZwR']"))
    print(elements_list)
    # How do I extract the text honhon?

If needed I could share the entire source code for reproduction.

>Solution :

You need iterate the list and then get the text value.

elements_list = EC.presence_of_all_elements_located((By.XPATH,"//a[@class='product-cardstyles__Link-sc-1uwpde0-9 bSQmwP hyperlinkstyles__Link-j02w35-0 coaZwR']"))
for element in elements_list:
    print(element.text)

if you want to get text values in a list use this.

print([element.text for element in WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.XPATH,"//a[@class='product-cardstyles__Link-sc-1uwpde0-9 bSQmwP hyperlinkstyles__Link-j02w35-0 coaZwR']")))])

Leave a ReplyCancel reply