【问题标题】:How can I get the last text in <p> through xpath in selenium webdriver如何通过 selenium webdriver 中的 xpath 获取 <p> 中的最后一个文本
【发布时间】:2019-10-28 17:39:12
【问题描述】:

我想提取屏幕截图中圈出的段落中的文本。但是从类的最后一个实例开始,因为这个文本是动态的。因此,我想要这堂课的最后一篇课文。

我试过了

String Reply2= driver.findElement(By.xpath("//div[@class='chat-message-content clearfix']/last[]")).getText(); ][1]

【问题讨论】:

    标签: java selenium xpath webdriver


    【解决方案1】:

    你不能使用 last[]

    使用 [last()]

    //div[contains(@class,'chat-message-content')][last()]/p
    

    【讨论】:

      【解决方案2】:

      你快到了,只需要在你的 xpath 中进行一些更改。

       String xPath = "(//div[@class='chat-message-content clearfix']/p)[last()]"
      

      //div[@class='chat-message-content clearfix']/p 应该获得chat-message-content clearfix 类下的所有p 项目。然后使用 () 对它们进行分组。现在使用[last()] 从组中获取最后一项。

      使用下面的行获取文本。

       String Reply2= driver.findElement(By.xpath(xPath)).getText();
      

      【讨论】:

        猜你喜欢
        • 2012-07-22
        • 1970-01-01
        • 2018-09-04
        • 2016-06-19
        • 2015-06-29
        • 1970-01-01
        • 2019-01-10
        • 2022-06-11
        • 1970-01-01
        相关资源
        最近更新 更多