The below code stored a list of the web element name. Is there any way to convert in string
List<WebElement>title=driver.findElements(By.xpath("//div[@class='document-card__details']//h3"));
The below code stored a list of the web element name. Is there any way to convert in string
List<WebElement>title=driver.findElements(By.xpath("//div[@class='document-card__details']//h3"));
Instead of storing the WebElements you may like to store the textContent
using Java8's stream()
and map()
you can use the following solution:
cssSelector:
List<String> titles = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.document-card__details h3"))).stream().map(element->element.getText()).collect(Collectors.toList());
xpath:
List<String> titles = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class='document-card__details']//h3"))).stream().map(element->element.getText()).collect(Collectors.toList());
You can find a couple of relevant discussions in: