如何在比较数组列表时删除空格

时间:2018-06-08 11:29:09

标签: java selenium automation

//View Company-View Project

driver.manage().timeouts().implicitlyWait(100000, TimeUnit.SECONDS);
List<WebElement> rows10 = driver.findElements(By.xpath("//*[@class='content- 
wrapper']/div[2]/div[1]/div[1]/div/p|//*[@class='content- 
wrapper']/div[2]/div[1]/div[4]/div/p|//*[@class='content- 
wrapper']/div[2]/div[1]/div[5]/div/p|//*[@class='content- 
wrapper']/div[2]/div[1]/div[7]/div/p|//*[@class='content- 
wrapper']/div[2]/div[1]/div[8]/div/p|//*[@class='content- 
wrapper']/div[2]/div[2]/div[6]/div/p"));
List<String> all_elements_text10 = new ArrayList<>();
for(int i=0; i<rows10.size(); i++) {
  all_elements_text10.add(rows10.get(i).getText());
  System.out.println(rows10.get(i).getText());
}

List<WebElement> rows11 = driver.findElements(By.xpath ("/html/body/div/div[2]/div[2]/div[1]/div[2]/div/p|/html/body/div/div[2]/div[2]/div[1]/div[3]/div/p|/html/body/div/div[2]/div[2]/div[1]/div[9]/div/p|/html/body/div/div[2]/div[2]/div[2]/div[1]/div/p|/html/body/div/div[2]/div[2]/div[2]/div[2]/div/p|/html/body/div/div[2]/div[2]/div[2]/div[3]/div/p|/html/body/div/div[2]/div[2]/div[2]/div[4]/div/p|/html/body/div/div[2]/div[2]/div[2]/div[5]/div/p"));
List<String> all_elements_text11 = new ArrayList<>();
for(int i=0; i<rows11.size(); i++) {
  all_elements_text11.add(rows11.get(i).getText());
  System.out.println(rows11.get(i).getText());
 }

Assert.assertEquals(all_elements_text10, all_elements_text11);
System.out.println("All Dropdown Fields matched in View Company-Project");


}

我在这里比较两个数组列表。通过执行上面的代码,我得到了以下错误。第二个数组列表有一个空格。如何删除该空格并比较列表。

java.lang.AssertionError: Lists differ at element [3]: Selenium != Selenium  expected [Selenium] but found [Selenium ]

1 个答案:

答案 0 :(得分:0)

试试这个。

rows10.get(i).getText().replaceAll("\\s+", "") 

rows11.get(i).getText().replaceAll("\\s+", "")

希望这有帮助!