[英]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.