Selenium find_elements一遍又一遍地返回相同的元素

时间:2020-11-06 11:28:05

标签: python selenium

尝试通过以下方式阅读Likedin feed的评论:

        comments = browesr.find_elements_by_xpath("//*[@class='comments-comments-list__comment-item comments-comment-item ember-view']")
        print(comments[0].id)
        print(comments[1].id)
        print(comments[2].id)
        print(comments[0].get_attribute("id"))
        print(comments[1].get_attribute("id"))
        print(comments[2].get_attribute("id"))
        print(comments[0].find_element_by_xpath("//*[@data-control-name='comment_actor_description']").text)
        print(comments[1].find_element_by_xpath("//*[@data-control-name='comment_actor_description']").text)
        print(comments[2].find_element_by_xpath("//*[@data-control-name='comment_actor_description']").text)

输出:

0.854587949237863-1
0.854587949237863-2
0.854587949237863-3
ember817
ember851
ember885
Ralph M.
out of network
3rd+
Managing Director at Intervi
Ralph M.
out of network
3rd+
Managing Director at Intervi
Ralph M.
out of network
3rd+
Managing Director at Intervi

您可以看到ID是不同的,但是每个ID中的元素都是相同的(第一个),这可能是什么问题?

1 个答案:

答案 0 :(得分:1)

只需替换XPath

"//*[@data-control-name='comment_actor_description']"

使用

".//*[@data-control-name='comment_actor_description']"