2

I want to get only the text "Invitation sent to xxxxx", I do not want to get what's inside the

<button class="action" data-ember-action="" data-ember-action-3995="3995">
          Visualizar perfil
 </button>

I'm getting the text this way:

String pessoapopu = driver.findElement(By.className("artdeco-toast-message")).getText();                
System.out.println(pessoapopu); 

Page structure:

<div class="artdeco-toast-inner" data-ember-action="" data-ember-action-3994="3994">
    <li-icon aria-hidden="true" type="success-pebble-icon" class="artdeco-toast-icon"><svg viewBox="0 0 24 24" width="24px" height="24px" x="0" y="0" preserveAspectRatio="xMinYMin meet" class="artdeco-icon"><g class="large-icon" style="fill: currentColor">
        <g id="success-pebble-icon">
          <g>
            <circle class="circle" r="9.1" stroke="currentColor" stroke-width="1.8" cx="12" cy="12" fill="none" transform="rotate(-90 12 12)"></circle>
            <path d="M15.667,8L17,9.042l-5.316,7.36c-0.297,0.395-0.739,0.594-1.184,0.599c-0.455,0.005-0.911-0.195-1.215-0.599l-2.441-3.456l1.416-1.028l2.227,3.167L15.667,8z" fill="currentColor"></path>
            <rect style="fill:none;" width="24" height="24"></rect>
          </g>
        </g>
        <g id="Layer_1">
        </g>
      </g></svg></li-icon>
    <p class="artdeco-toast-message">
              Invitation sent to xxxxx
        <button class="action" data-ember-action="" data-ember-action-3995="3995">
          Visualizar perfil
        </button>

    </p>
  </div>

image

4
  • Wait for the element to be visible and try the same code.. A guess. Commented Jul 25, 2017 at 12:56
  • @Paulo, What you getting as output ? complete text including button text ? or any error ? Commented Jul 25, 2017 at 12:56
  • @Tuks I get the full text, even what's inside the button, which is text irrelevant to me. Commented Jul 25, 2017 at 13:00
  • @santhoshkumar I already do it rs Commented Jul 25, 2017 at 13:01

4 Answers 4

7

You can use //p[@class='artdeco-toast-message']/text() xpath to locate the Invitation sent to xxxxx text but selenium doesn't support text() method in xpath to locate a text node.

Either if you try to locate the element using below xpath to exclude the button text by using not() function of xpath :

//p[@class='artdeco-toast-message']/node()[not(self::button)]

Again it locating the element using text node so Selenium doesn't allow this

Here one solution available to execute same xpath i.e. JavascriptExecutor

Use evaluate() method of JavaScript and evaluate your xpath using JavascriptExecutor

JavascriptExecutor js = (JavascriptExecutor)driver;
Object message = js.executeScript("var value = document.evaluate(\"//p[@class='artdeco-toast-message']/text()\",document, null, XPathResult.STRING_TYPE, null ); return value.stringValue;");
System.out.println(message.toString().trim());

OR

JavascriptExecutor js = (JavascriptExecutor)driver;
Object message = js.executeScript("var value = document.evaluate(\"//p[@class='artdeco-toast-message']/node()[not(self::button)]\",document, null, XPathResult.STRING_TYPE, null ); return value.stringValue;");
System.out.println(message.toString().trim());

It will give you the expected result. No need to get all data and then formatting using String functions.

You can explore evaluate() in detail from here

Sign up to request clarification or add additional context in comments.

Comments

1

In fact Selenium's getText() method retrieves all the text in sub elements too, so the approach, recommended by Murthi is the most applicable, as far as I know. Try this approach:

String newLine = System.getProperty("line.separator");
String pessoapopu = driver.findElement(By.className("artdeco-toast-message"))
                           .getText().replaceAll(newLine, "");

Or try/ask for more convenient HTML code.

3 Comments

is there any way to handle this using only xpath ?
I'm thinking of 'and not()' xpath method, but not sure about it.
This doesn't do what OP asked... I'm not sure why it's marked as the answer.
0

You can try with the following code.

WebElement text=driver.findElement(By.className("artdeco-toast-message"));
String wholeText = text.getText();
String unWantedText=text.findElement(By.className("action")).getText();
String RequiredText=wholeText.replace(unWantedText,"");
System.out.println(RequiredText);

3 Comments

Did not work "The method getText() is undefined for the type By"
It worked in parts, it is tripling the mesnagem in the console: Invitation sent to Mauro Santos Invitation sent to Mauro Santos Invitation sent to Mauro Santos and it does not find the element in the next pass no such element: Unable to locate element: {"method":"class name","selector":"action"}
may be the button with the class name "action" is not there in the next pass. You can check the html code or give us the full code.
0

Given the HTML, you should be able to .getText(), split that text on a newline, and then take the first split to get the text you want.

driver.findElement(By.cssSelector("p.artdeco-toast-message")).getText().split("\r\n")[0];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.