简体   繁体   中英

How to replace/remove comma from a tezt using Selenium+java?

My Webpage contains text like 'User,'. I need to capture this text without comma. When i am doing assertEquals between the user entered text(User) and text retrieved from the webpage(User,), it failed because of extra comma.Can you help how to replace or remove that comma and getText?

Using the below xpath, i am capturing the text, driver.findElement(By.xpath("//div[@id='mainContents']/div[2]/div/table/tbody/tr/td")).getText();

Let's say you place your text in t and you only want to remove the last comma (if present)

    t=driver.findElement(By.xpath("//div[@id='mainContents']/div[2]/div/table/tbody/tr/td")).getText();
    if(t.charAt(t.length()-1).equals(","))
        t=t.substring(0,t.length()-2));

只需使用String.replace

driver.findElement(By.xpath("//div[@id='mainContents']/div[2]/div/table/tbody/tr/td")).getText().replace(",", "")
string usertext=driver.findElement(By.xpath("//div[@id='mainContents']/div[2]/div/table/tbody/tr/td")).getText();     
string actual = usertext.replace(",");  
Assert.equals(expected, actual );

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