首页 > 解决方案 > 无论如何我可以在不使用标记名的情况下获得 href 吗?

问题描述

def get_posts(driver):
posts = []
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.TAG_NAME,"a")))

links_of_posts = driver.find_elements_by_tag_name('a')     # gets all elements with tag a and puts them in a list

for links in links_of_posts:
    post = Post()
    if hasattr(links,'href'):   #ERROR: no elements successfully enter the if, removing the if causes a stale element reference exception
        post.link = links.get_attribute('href')
        if '/p/' in post.link:
            posts.append(post)

所有元素的 DOM 结构都应该是这样的,除了 href 不同:...

我使用的页面是 Instagram 个人资料页面,所以https://www.instagram.com/definitelynotbot24/

links_of_posts 包含应该有超链接的元素,但似乎没有一个元素这样做,有什么提示吗?

标签: pythonpython-3.xseleniumselenium-chromedriver

解决方案


推荐阅读