首页 > 解决方案 > 如何使用 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","");

提前致谢,

标签: javaselenium-webdriver

解决方案


用这个:

//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();

推荐阅读