Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Acessing multiple anchor elements inside text

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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']")))])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading