I am not able to convert webelement to string values.
List<WebElement> a=driver.findElements(By.xpath("//*[@id='content']/table/tbody/tr/td[2]"));
i want convert above List<WebElement>
to String
.
I am not able to convert webelement to string values.
List<WebElement> a=driver.findElements(By.xpath("//*[@id='content']/table/tbody/tr/td[2]"));
i want convert above List<WebElement>
to String
.
You can do something like the following:
List<WebElement> links =driver.findElements(By.xpath("//*[@id='content']/table/tbody/tr/td[2]"));
String []linkText =new String[links.size()];
int i=0;
//Storing List elements text into String array
for(WebElement a: links)
{
linkText[i]=a.getText();
i++;
}
Here's another way you can do it using java 8 features:
By byXpath = By.xpath("//*[@id='content']/table/tbody/tr/td[2]");
List<WebElement> links = driver.findElements(byXpath);
List<String> texts = links.stream().map(WebElement::getText).collect(Collectors.toList());
This will iterate through each WebElement, call getText, and collect all the returned values into a brand new list.
@FindBys({
@FindBy(xpath = "//div[@class=\"error-text text--center margin--bottom_10\"]")
})
private List<WebElement> errorTexts;
private List<String> getAllErrorsText() {
return errorTexts.stream()
.map(WebElement::getText)
.collect(Collectors.toList());
}