简体   繁体   English

如何使用Selenium + java从tezt替换/删除逗号?

[英]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? 当我在用户输入的text(User)和从网页检索的文本(User,)之间执行assertEquals时,由于多余的逗号而失败。您能帮忙替换或删除该逗号和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(); 使用下面的xpath,我正在捕获文本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并且只想删除最后一个逗号(如果有)

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

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

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