如何获取 HTML <p>(段落)元素中的文本

How to get the Text inside HTML <p> (paragraph) element

下面是单选按钮的 HTML 片段。我想使用 selenium Webdriver.Unable 获取 'Select from list' 以使用 getText()

获取
 <p> <input type="radio" checked="checked" value="1" name="selectedItem"> Select By Audit ID </p>
 <p> <input type="radio" value="2" name="selectedItem"> Select from list of My Open Audits </p>
 <p> <input type="radio" value="3" name="selectedItem"> Select from list of All Open Audits </p>

你可以driver.findElement(By.cssSelector("p")).getText()。这将从 p 元素中获取文本。

如果要查找基于 input 的元素,请使用以下 xpath,然后使用 getText()

//input[@name='selectedItem']/..

编辑

//input[@name='selectedItem'][@value='1']/..

由于 p 标签是 input 的父标签,而文本 'Select from list' 是 p 标签的 innerHTML/text,下面的 JAVA 代码可能会有所帮助(假设您使用的是 java):

String text = driver.findElement(By.xpath("//input[@name='selectedItem']/..")).getText();

这将检索 p 标签的 innerHTML/text。