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&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0CC0QFjAB&url=http%3A%2F%2Fwww.dongemondcollege.nl%2F&ei=ihIwVdTqKtDYaoSjgMAP&usg=AFQjCNEvpxj60GxhQekQ2qI6QXDP2Vso1g&sig2=DuKoiCbIcI0ncx8D4gnSaA&bvm=bv.91071109,d.bGQ"
onmousedown="return rwt(this,'','','','2','AFQjCNEvpxj60GxhQekQ2qI6QXDP2Vso1g','DuKoiCbIcI0ncx8D4gnSaA','0CC0QFjAB','','',event)"
data-href="http://www.dongemondcollege.nl/">
Dongemond College > Algemeen > 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
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
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP