java - 仅从父级 <td> 获取文本

标签 java selenium xpath selenium-webdriver

在网站中它有这样的页脚

<td class=resutinfo >
    Displaying 1 - 10 of 41 records Results per page: 
    <select onchange="javascript:gotoRow(1);" name="pageSize>
        <option value=10, selected="true> 10 </option>
        <option value=20, > 20 </option>
        <option value=10, > 30 </option>
    </select>
</td>

问题:

当我使用getText()时为此<td> ,它显示的值是这样的

Displaying 1 - 10 of 41 records Results per page: 10 20 30

但我只需要我不想要的意思,选择选项。

Displaying 1 - 10 of 41 records Results per page:

请您提供如何仅获取该字符串的建议。

最佳答案

这个想法是你得到文本 <td class="resutinfo">首先,应该是

Displaying 1 - 10 of 41 records Results per page: 10 20 30

然后你会得到 <select> 的文本,这应该是

10 20 30

然后将其替换为空字符串以删除。

String tdText = driver.findElement(By.xpath(".//td[@class='resutinfo']")).getText();
String selectText = driver.findElement(By.xpath(".//td[@class='resutinfo']/select")).getText();
tdText.replace(selectText, "").trim();

关于java - 仅从父级 <td> 获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21770373/

相关文章:

javascript - Python Selenium : Possible to change value when clicking option in dropdown menu?

python - 使用 Selenium/Python 的 CSS 选择器中的 OR 条件

java - 导航 View 不在工具栏中

java - 传递给新 Activity 的数据为空

Python Selenium 将列表项保存为数组

javascript - Selenium IDE - 错误 : Permission denied for <http://www. example.com> 获取属性 Location.href

java - 图形数据流组合框架

java - 使用模型类 Android 从 Firebase 访问用户信息

xml - 带有根元素中属性的 xml 的 xsl 不起作用

python - 在 Selenium PYTHON 中使用 span id 从下拉列表中选择一个值