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
asked Apr 17, 2015 at 7:51
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. Commented 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

answered Apr 17, 2015 at 8:11
0

This works

botonAceptar = driver.find_element_by_xpath('//a[@id="close_entrance_terms"]').click()
IAmMilinPatel
7,7667 gold badges44 silver badges68 bronze badges
answered Jul 21, 2020 at 1:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.