I need to get the text of a td element with selenium. I have problem with extract text, I just receive null. I tried used list, getText()
and so on. The HTML code is on the picture and element looks like you can see on the picture.
getDriver().findElement(By.xpath("//*[@id=\"standortTable\"]/tbody/tr/td[2]")).isDisplayed();
String test = getDriver().findElement(By.xpath("//*[@id=\"standortTable\"]/tbody/tr/td[2]")).getText();
System.out.println(test);
But I receive NULL, just "".
CodePudding user response:
I guess you are missing a wait I.e. you trying to read the element text content before it is completely rendered.
Try this:
WebDriverWait wait = new WebDriverWait(getDriver(), 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@id='standortTable']//td[@aria-colindex='2']")));
String test = getDriver().findElement(By.xpath("//table[@id='standortTable']//td[@aria-colindex='2']")).getText();
System.out.println(test);
CodePudding user response:
The element is an Angular element, so to extract the text Teststandort1 you have to induce WebDriverWait for the visibilityOfElementLocated()
and you can use either of the following Locator Strategies:
Using xpath and
getText()
:System.out.println(new WebDriverWait(getDriver(), 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@id='standortTable']//tbody//tr[@class='ng-star-inserted']//td[@aria-colindex='2']"))).getText());
Using cssSelector and
getAttribute("innerHTML")
:System.out.println(new WebDriverWait(getDriver(), 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table#standortTable tbody tr.ng-star-inserted td[aria-colindex='2']"))).getAttribute("innerHTML"));