java - 如何使用 Selenium 从 xpath 中删除特殊字符?
问题描述
如您所见,我使用了一个动态 xpath://td[text()='Discharge Air']/following-sibling::td/span
从 zone1 到 zone3,但是当我使用 gettext() 仅获取 100 但特殊字符°F 也即将到来。因此请建议如何删除这个特殊字符 °F,因为我只想要来自这个 xpath 的数据 100?正如您在图像中看到的,只有 1 个跨度可用,所以我也无法分离跨度。
String s = driver.findElement(By.xpath("//td[text()='Discharge Air']/following-sibling::td/span")).getText();
s.replace("°F","");//用空字符串替换°F
我可以使用 List 而不是 String,因为所有这些 xpath 都是相同的类型,因此我可以直接编写,然后我可以使用 for 循环 for getText()。
List s=driver.findElements(By.xpath("//td[text()='Discharge Air']/following-sibling::td/span"));
s.replace("°F","");
提前致谢,
解决方案
用这个:
//first find the elements and save it as you did (with the xpath you posted)
String s = driver.findElement(By.xpath("//td[text()='Discharge Air']/following-sibling::td/span")).getText();
s.replace("°F","");//replace the °F with empty string
如果您发现字符串上仍有空格,您可以使用它来删除它们:
s.trim();
推荐阅读
- javascript - 在javascript中突出显示文本按钮
- python - 如何在 python 虚拟环境中安装 rasa x - 使用 pip3 的 Rasax 安装错误
- c# - 使用像大纲步骤这样的背景步骤
- gnuplot - GNUPLOT - 如何检查是否设置了“multiplot”
- c# - Interop.Outlook / UiPath - 有没有办法以异步方式发送电子邮件?
- arduino - 为什么这个 Arduino 代码打印“拒绝访问”
- ios - 快速画一条全宽的虚线?
- php - Magento 2 - 类别属性保存到错误的商店 ID
- typescript - 定义没有 `as` 关键字的具体函数返回类型
- blueprism - 有没有一种方法可以将 10 个数据项添加到 BluePrism 中的集合中?