I have following piece of code to read value from one field:
private final String dataRowXpath = "*//div[@ref='eCenterContainer']//div[@role='row']";
private final String nbrCellXpath = "//div[@col-id='tradeCount']//span"
List<WebElement> dataRows = driver.findElements(By.xpath(dataRowXpath));
WebElement row = dataRows
.stream()
.filter(row -> row.getAttribute("row-id").equals("USD"))
.findFirst()
.get();
String text = row.findElement(By.xpath(nbrCellXpath)).getText()
In this case I receive an empty string.
If I do it in this way with long xpath it reads correct value:
String xpath = "*//div[@ref='eCenterContainer']//div[@role='row' and @row-id='USD']" nbrOfCellXpath;
WebElement element = driver.findElement(By.xpath(xpath));
String text = element.getText();
So my question is why using findElement at WebElement row doesn't work.
I'd like to do it this way because I need to read more values from specific row elements so it would be better to have possibility to separate needed row first.
CodePudding user response:
In order to find element inside element with XPath locator the XPath locator should start with a dot .
.
So, in order to locate a cell inside a row with
String text = row.findElement(By.xpath(nbrCellXpath)).getText()
please try defining the nbrCellXpath
XPath expression as
".//div[@col-id='tradeCount']//span"