If you are looking to locate the text
The new customer role has been added successfully.
using xpath, you can use the following code:
//div[@class='alert alert-success alert-dismissable']/text()
. However, it's important to note that selenium does not support the
text()
method in xpath for locating a text node.
To extract text from within a <button>
tag, you can try the code below:
String button = driver.findElement(By.xpath("//button[@class='close'][@type='button']")).getText();
System.out.println(button);
If you specifically want to retrieve the text
The new customer role has been added successfully.
, you can try this code snippet:
JavascriptExecutor js = (JavascriptExecutor)driver;
Object str = js.executeScript("var value = document.evaluate(\"//button[@class='close']/following::node()[contains(., 'The new customer role has been added successfully.')]\",document, null, XPathResult.STRING_TYPE, null); return value.stringValue;");
System.out.println(str.toString());
You can find more information on Document.evaluate method here.
For visual reference, please see the image below:
https://i.sstatic.net/Ki6Tx.png