0

I want to get a single line of text (e.g. standard_user). How can i do this?

<div id="login_credentials" class="login_credentials">
              <h4>Accepted usernames are:</h4>

              standard_user
<br>
              locked_out_user
<br>
              problem_user
<br>
              performance_glitch_user<br>

            </div>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
noon
  • 161
  • 1
  • 1
  • 3

5 Answers5

1

Collect all the text contained in one div, and separate them with break lines:

WebElement element = driver.findElement(By.id("login_credentials"));
String lines[] = element.getText().split("\n");
System.out.println(lines[1]);
frianH
  • 7,295
  • 6
  • 20
  • 45
0

you have to read the File line by line and set a condition to get the line with no Tag

if(!line.startwith("<"){ //your code}

or use a library for reading html file depend on your programming language

elouanesbg
  • 66
  • 1
  • 8
0

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.

CEH
  • 5,701
  • 2
  • 16
  • 40
0

Just get the div content and use some string manipulation with line break.

String text=driver.findElement(By.cssSelector("div#login_credentials")).getText();
String lines[] = text.split("\\r?\\n");
System.out.println(lines[1]);
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

The text standard_user is a text node with in the <div> node. So to extract the text standard_user you can use either of the following Locator Strategies:

  • Using cssSelector:

    System.out.println((String)((JavascriptExecutor)driver).executeScript('return arguments[0].childNodes[2].textContent;', new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.login_credentials#login_credentials")))));
    
  • Using xpath:

    System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].childNodes[2].textContent;', new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='login_credentials' and @id='login_credentials']")))).toString());
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352