I am writing a selenium test on our web page. There is one label field that has one to many text segments separated by a right-caret icon. I am trying to extract the individual text segments from the label into a list.
This is what the html looks like in the DOM. In this case there are 3 individual text segments: "MainSchedule", "Container1", and "Container1.2".
<p class="MuiTypography-root MuiTypography-body1" style="word-break: break-all;">
"MainSchedule"
<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="caret-right" class="svg-inline--fa fa-caret-right fa-w-6 sm-icon" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 192 512" style="margin: 0px 5px;">
<path fill="currentColor" d="M0 384.662V127.338c0-17.818 21.543-26.741 34.142-14.142l128.662 128.662c7.81 7.81 7.81 20.474 0 28.284L34.142 398.804C21.543 411.404 0 402.48 0 384.662z"/>
</svg>
"Container1"
<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="caret-right" class="svg-inline--fa fa-caret-right fa-w-6 sm-icon" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 192 512" style="margin: 0px 5px;">
<path fill="currentColor" d="M0 384.662V127.338c0-17.818 21.543-26.741 34.142-14.142l128.662 128.662c7.81 7.81 7.81 20.474 0 28.284L34.142 398.804C21.543 411.404 0 402.48 0 384.662z"/>
</svg>
"Container1.2"
</p>
I can easily get the paragraph object with
WebElement label = WebDriver.findElement(By.cssSelector("p.MuiTypography-root"))
but when I try to do a getText() off of label it returns all 3 of the text segments in one string with no breaks to show where the image icons are.
Using the Chrome tools I can look at the element's properties and on the "p.MuiTypography-root" I see the "firstChild" text content is the first text segment "MainSchedule". I have tried
label.findElement(By.xpath("first-child"))
and it just throws an error. From that "firstChild" I can step through the "nextSibling" in the Chrome tools and find the ones that hold the individual text segments. But I have not figured out how to code this to read them.
I am writing my tests in java.