python selenium xpath查找并获取第二个元素

python selenium xpath查找并获取第二个元素,python,selenium,xpath,Python,Selenium,Xpath,构造第二个元素的路径的问题。 如何构建路径 我试图用xPath查找第二个元素 getOpis = driver.find_element_by_xpath("(//article[2]/div/div/descendant-or-self::*)").text 这种方法行不通 下面的方法也不起作用 getOpis = driver.find_element_by_xpath("//article[2]/div/div/descendant-or-self::*)[2]").text 对于第一

构造第二个元素的路径的问题。 如何构建路径

我试图用xPath查找第二个元素

getOpis = driver.find_element_by_xpath("(//article[2]/div/div/descendant-or-self::*)").text
这种方法行不通

下面的方法也不起作用

getOpis = driver.find_element_by_xpath("//article[2]/div/div/descendant-or-self::*)[2]").text
对于第一个元素,使用:

getOpis = driver.find_element_by_xpath("//article/div/div/descendant-or-self::*)").text
已为第一个元素正确下载该元素。 我做错了什么,我不能得到第二项。 路径必须从第二个项目元素检索当前文本

File "C:\Users\media\PycharmProjects\test\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"(//article[2]/div/div/descendant-or-self::*)"}
  (Session info: chrome=74.0.3729.169)

我同意约翰·戈登和金·斯通的观点。如果没有更多细节,很难判断,但您可以尝试:

getOpis = driver.find_elements_by_xpath("//article/div/div/descendant-or-self::*").text
使用元素而不是元素将返回元素列表。然后,您可以使用:

getOpis[1]


获取第二个元素。

如果不查看html源代码,我们就无法判断错误所在。请与我们分享您的示例。我使用一个程序来报告当前添加的帖子。来自facebook。该公司facebook页面的手机外观。有一个密码。程序会区分帖子是否被钉住,然后必须下载第二篇帖子的内容。类似这样的内容:删除了xpath末尾的额外“)”。我转换为:我仍然下载第一篇文章。有什么建议吗?getOpis=driver。通过xpath(“//article/div/div/genderant或self::*”)查找元素getOpis=getOpis[2]。文本