1) I would try as alternative CSS selector approach and create 5 WebElement Lists ( already in split representation for searched fields):
List<WebElement> fullNames = driver.findELements(By.cssSelector("ul.displayAddressUL li.AddressFullName>b"));
List<WebElement> addressLines= driver.findELements(By.cssSelector("ul.displayAddressUL li.AddressAddressLine"));
List<WebElement> cities= driver.findELements(By.cssSelector("ul.displayAddressUL li.AddressCity"));
List<WebElement> countries=driver.findELements(By.cssSelector("ul.displayAddressUL li.country"));
List<WebElement> phoneNums=driver.findELements(By.cssSelector("ul.displayAddressUL li.phone"));
for (int i=0;i<fullNames.size(); i++){
String fullName= fullNames.get(i).getText();
String addressLine =addressLines.get(i).getText();
String city = cities.get(i).getText();
String county = countries.get(i).getText();
String phoneNumber = phoneNums.get(i).getText();
}
Also I'd like to add:
CSS selectors perform far better than Xpath and it is well documented in Selenium community. Here are some reasons,
- Xpath engines are different in each browser, hence make them
inconsistent
- IE does not have a native xpath engine, therefore selenium injects
its own xpath engine for compatibility of its API. Hence we lose the
advantage of using native browser features that WebDriver inherently
promotes.
- Xpath tend to become complex and hence make hard to read in my
opinion However there are some situations where, you need to use
xpath, for example, searching for a parent element or searching
element by its text (I wouldn't recommend the later).
More details on performance comparison you can get here
2) another alternative approach that I would recommend as mentioned here
try to use HtmlUnitDriver
, that will definitely increase your performance;
OR:
to use pure js wrapped by Javascipt executor for text extraction like:
String elem_css_selector="blablabla...";
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\""+elem_css_selector+"\");");
stringBuilder.append("return x.text().toString();") ;
String resultingText= (String) js.executeScript(stringBuilder.toString());
Assert.assertTrue(resultingText.trim().equals("Expected Text") );