-1

I have this in my code:

link_tag = "//div[@class= 'yuRUbf']//a/@href"

With this code I get this error

The result of the xpath expression "//div[@class= 'yuRUbf']//a/@href" is: [object Attr]. It should be an element.

I don't know any other way to scrape the URL from that div class. How can I fix this? This code works fie on the scraper chrome extension but not on python.

Markus Ham
  • 31
  • 4
  • Does this answer your question? [How to get attribute of element from Selenium?](https://stackoverflow.com/questions/30324760/how-to-get-attribute-of-element-from-selenium) – JaSON Jun 07 '22 at 08:57

2 Answers2

1

You have to change your XPath to select an element node (as the error message suggests) - and not an attribute node - and, after that, get its attribute. So use

link_tag = "//div[@class= 'yuRUbf']//a"
links = driver.find_elements_by_xpath(link_tag)

and then extract the attribute with

links[0].get_attribute("href")

to get the @href attribute of the first matching element.

zx485
  • 28,498
  • 28
  • 50
  • 59
0

This solution might work.

div = driver.find_element(By.CSS_SELECTOR,"div[class='yuRUbf']")
url = div.get_attribute("href")
ageow
  • 21
  • 4