Search code examples
pythonseleniumxpathhrefelement

Python Selenium - href Attribute outputs a false link


Im using Selenium to extract a href elements under div class elements from comments on Facebook posts.

To do that im using https://m. and not https://www.

The weird thing is that the code is working but it produces a wrong href link.

This is my test link: https://m.facebook.com/permalink.php?story_fbid=10158970984237798&id=267767252797

here's the relevant sample from the code:

links = browser.find_elements_by_xpath("//div[@class='_2b1h async_elem']/a")
for link in links:
   print(link.get_attribute('href'))

This is the first print (wrong):

https://m.facebook.com/comment/replies/?ctoken=10158970984237798_10158971018182798&count=7&curr&pc=1&isinline&initcomp&ft_ent_identifier=10158970984237798&eav=AfYL7kFupIufaUdT64Uj85QVZhOZxYUkTTY1wrjRnMqxFG85Nmev-Au_bPm0a4Z0HzM&av=100078743166486&gfid=AQC0Hgp2a-I6Q7Fpj_Y&tn=R

but the href link should be (Right):

https://m.facebook.com/comment/replies/?ctoken=10158970984237798_10158971018182798&count=7&curr&pc=1&isinline&initcomp&ft_ent_identifier=10158970984237798&eav=AfaIC6mT5kvBUEIfgoLYj9G5KYF_lv4sncnOMaJjJKk1dEk-aXbNnYwwNnoFmt9kIOQ&av=100000431416784&gfid=AQDiD_GY8uckLbNf0bQ&tn=R

Why does this piece of code get the wrong href link?


Solution

  • For everyone looking for a solution to the same problem I had, i will save you the trouble and say that after a lot of digging, it appears that FB is simply changing several elements values (like href..) after a varied short period of time (can be hours or minutes).

    in my case, the code worked only with 30 min gap between runs.

    FYI, Peace.