TransWikia.com

How to get data-href instead of href with selenium in python?

Software Quality Assurance & Testing Asked on October 25, 2021

I have currently a python code that should grab a link from google.
However, google uses a somewhat different method to linking. how could I grab the data-href instead of just href.

this is the html example of a google link, the code is different when I use firefox.. there is no data-href:

<a 
 href="/url?    sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=2&amp;cad=rja&amp;uact=8&amp;ved=0CC0QFjAB&amp;url=http%3A%2F%2Fwww.dongemondcollege.nl%2F&amp;ei=ihIwVdTqKtDYaoSjgMAP&amp;usg=AFQjCNEvpxj60GxhQekQ2qI6QXDP2Vso1g&amp;sig2=DuKoiCbIcI0ncx8D4gnSaA&amp;bvm=bv.91071109,d.bGQ"
 onmousedown="return rwt(this,'','','','2','AFQjCNEvpxj60GxhQekQ2qI6QXDP2Vso1g','DuKoiCbIcI0ncx8D4gnSaA','0CC0QFjAB','','',event)"
 data-href="http://www.dongemondcollege.nl/">
 Dongemond College &gt; Algemeen &gt; Home
</a>

Below is the Python code that should grab the link

Any suggestions?

def getLinks(source):

    websiteLinks = []
    for link in source.find_all('a'):
        url = link.get('href')
        if url:
            if '/search?' not in url:
                websiteLinks.append(url)
    return websiteLinks

2 Answers

This works

botonAceptar = driver.find_element_by_xpath('//a[@id="close_entrance_terms"]').click()

Answered by Jorge Gonzalez on October 25, 2021

data-href is an attribute of a HTML tag, try to use get_attribute method, something like:

url = browser.find_element_by_xpath("//a").get_attribute("data-href")

For more information see: http://selenium-python.readthedocs.org/en/latest/api.html#selenium.webdriver.remote.webelement.WebElement.get_attribute

Or this for examples: https://stackoverflow.com/questions/12579061/python-selenium-find-object-attributes-using-xpath

Answered by Niels van Reijmersdal on October 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP