繁体   English   中英

如何使用 Selenium 从 xpath 中删除特殊字符?

[英]How to remove special characters from a xpath using Selenium?

在此处输入图片说明

如您所见,我使用了一个动态 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();

List disch_Air = driver.findElements(By.xpath("//td[text()='Discharge Air']/following-sibling::td/span"));

 for(int i=0;i<disch_Air.size();i++) {
     
     System.out.println(disch_Air.get(i).getText().replace("°F", ""));
 }
    
    
     }

这就是我想要的,它工作正常,非常感谢你们的帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM