I would use a few lines to process the text -- it's a bit tricky with <br>
elements and elements containing partial text:
// first, get all the text by locating div element
string allText = driver.findElement(By.id("login_credentials")).getText();
// then get H4 text so we can remove this string
string textToRemove = driver.findElement(By.xpath("//div[@id='login_credentials']/h4")).getText();
// remove unwanted "Accepted usernames are:" text
string filteredText = allText.Replace(textToRemove, "");
// split filteredText on newline regex so we can get line items including 'standard_user'
string[] textArray = filteredText.split("\\r?\\n");
// get standard_user text by getting first item in the split array
string standardUserText = textArray[0];
The last 3 lines of this code can be simplified, but I wrote out this longer version of it so we can understand what is happening in each step.
allText
variable after evaluation should be equal to Accepted usernames are: standard_user locked_out_user problem_user performance_glitch_user
.
Once we remove the Accepted usernames are:
text that appears in the h4
element, filteredText
is equal to standard_user locked_out_user problem_user performance_glitch_user
with each item separated by a newline, either \r
or \n
character -- we use a regex to handle both cases..
We split filteredText
on \n
character so we get an array as follows:
[ "standard_user", "locked_out_user", "problem_user", "performance_glitch_user" ]
Then, we can call textArray[0]
to get first item in list, which should be standard_user
.