1

I am using Selenium to verify that a new user role has been successfully created, but, when I run Selenium (Java) in headless mode I am getting randomly getting empty strings returned in getText()

public List<String> getRoles() {
    String allRolesXpath = String.format("%s%s", TABLE_XPATH, ROLES_XPATH);

    List<WebElement> tableElements = webDriver.getWebDriver().findElements(By.xpath(allRolesXpath));

    List<String> roles = new ArrayList<>();

    for (WebElement element : tableElements) {
        String role = WebCtrlFactory.getInstance()
                .getByElement(TextCtrl.class, element).getText();
        roles.add(role);
    }

    return roles;
}

I am using the Chrome web driver as the website only supports Chrome based browsers. I am not sure if there are many others I can move to as as it checks for Chro(e)|(ium) based browsers.

I have read numerous other posts about people seeing this issue (this, this), but I haven't found a workable solution. Any suggestions or help would be very welcome.

Swatcat
  • 73
  • 6
  • 21
  • 57
  • Did you have a go at using `element.getAttribute("innerText")` or `element.getAttribute("value")` from one of the linked question, what were your results? Which selenium driver are you using, and did you try a different one? Please show an example of the raw data, or what is contained in `tableElements` or returned by `getByElement(TextCtrl.class, element)` Add your own debugging to see this data and you should quickly find why you have this issue (likely `findElements(By.xpath(allRolesXpath))` is grabbing empty or unrelated elements that you were not aware of). – sorifiend Jul 14 '22 at 23:52
  • @sorifiend I have added information about the driver, I have not checked element.getAttribute("value"), but I did try InnerText. – Swatcat Jul 15 '22 at 08:38
  • 1
    @PaulMorriss see https://stackoverflow.com/questions/59407469/selenium-katalon-can-not-get-webelements-text-when-running-in-jenkins-or-headle – pburgr Jul 15 '22 at 10:25

0 Answers0