2

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
1
  • Dongemond College &gt; Algemeen &gt how to get this value??? i mean i want to print this value...also assume this value keep changing. Jan 25, 2018 at 12:52

2 Answers 2

3

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

0

This works

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.