简体   繁体   中英

How to remove special characters from a xpath using Selenium?

在此处输入图片说明

As you are able to see, I have used one dynamic xpath: //td[text()='Discharge Air']/following-sibling::td/span to go from zone1 until zone3, but when I am using gettext() to fetch only 100 but special character °F is also coming. Hence please suggest how to remove this special character °F , because I want only data 100 from this xpath? As you can see in the image, only 1 span is available, so I can't separate span also.


String s = driver.findElement(By.xpath("//td[text()='Discharge Air']/following-sibling::td/span")).getText();

s.replace("°F","");//replace the °F with empty string

Instead of String, can i use List because all these xpath are of same type,hence directly i can write and afterwards i can use for loop for getText().

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

s.replace("°F","");

Thanks in advance,

Use this:

//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

and if you see that there are still spaces on your string you can use this to remove them:

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", ""));
 }
    
    
     }

This is what i want and its working fine thank you so much guys for ur help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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