Tips for retrieving text within a Div and button element

<div class="content-wrapper" style="min-height: 611px;">
  <div class="alert alert-success alert-dismissable">
    <button class="close" type="button" data-dismiss="alert" aria-hidden="true">×</button>
    The addition of the new customer role was successful.
</div>

I have been trying various methods to retrieve the text following the button tag, but so far, I have not been successful.

//div[@class='alert alert-success alert-dismissable']

Answer №1

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

Answer №2

If you're uncertain about your HTML structure, consider testing out this XPath suggestion:

//*[@class='alert alert-success alert-dismissable']

To retrieve text from the HTML using Java, utilize getText():

driver.findElement(By.xpath(" //*[@class='alert alert-success alert-dismissable']")).getText();

Please inform me if the XPath is functioning correctly for your purposes.

Answer №3

It seems like you've attempted using the following xpath:

//*[@class='alert alert-success alert-dismissable']

Can you verify if the alert box (assuming it's an alert box based on the provided html) appears as a new window? If so, you may need to switch to that new window.

Additionally, check for any overlay attribute associated with the alert pop-up.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using parameters in JQuery

I'm working on implementing smooth scrolling on my page, but I've been struggling with passing parameters in jQuery. My main goal is to obtain the position of specific divs on the page. I attempted a solution like the one below, but unfortunately ...

Tips on extracting values from HTML utilizing a CSS selector in instances where only class and style attributes are present. The class in question is generic and may be utilized elsewhere as well

''' 29/11/2021 ''' I have a code snippet that retrieves dates from a table on a webpage, but it also fetches other values along with the date. I am looking for a way to extract just the d ...

Error: Unable to submit data as the function this.submitData is not recognized

Having trouble calling an async function in the mounted() lifecycle hook of Vue.js? Keep getting the error message: Uncaught TypeError: this.submitData is not a function. Here's the code snippet in question: <template> <section class=&quo ...

Generate HTML elements within an AngularJS directive and establish a click event

Can you show me how to create some DOM elements and add a click event to them using an AngularJS directive? My current approach is as follows: var list = document.createElement("div"); list.classList.add('myList'); for(var i = 0; i < n; i++) ...

How to dynamically generate <select> <option> elements in VueJS using the v-for directive

Just started exploring VueJS and I recently learned how to use a v-for loop to populate a Select Options box. <select> <option v-for="person in persons" :value="personid">{{ personname }}</option> </select> ...

The squares in Threejs are presented in the form of rectangles

Currently, I am working on a vue-cli project that involves three.js. In this project, I have created three square surfaces that are perpendicular to each other, with each side measuring 2 units. To view and control the camera's perspective, I have imp ...

Having encountered an Unexpected token in my App.js file, I am seeking guidance on how to resolve

Encountering an error when attempting to run the npm start command in React. The error message is as follows: export default App; Syntax error: D:/react/react-sidebar-v1/src/App.js: Unexpected token (11:5) 9 | function App() { 10 | return ( > ...

Are there specific files or classes that store constants for different keyboard events?

When working in Angular, I often bind data with a host listener using code similar to the example below: @HostListener('window:keyup', ['$event']) onKeyUp(event: KeyboardEvent) { if (event.keyCode === 13) { this.onEnterClicked(ev ...

Tips for implementing a handler on a DOM element

$(document).ready(function(){ var ColorBars = document.getElementsByClassName("color-bar"); var number = 0; ColorBars[0].onclick = hideLine(0); function hideLine(index){ var charts = $("#line-container").highcharts(); v ...

The chosen index in the Material Stepper feature is experiencing a malfunction

I've been busy working on a Mat-Stepper, actually two of them. I have a component that contains two ng-templates set up like this: Question: Why is my selected index not functioning as expected? Am I missing something? I know you can define [selected ...

Is it possible to input tab characters in a TextField?

I am working with a multi-line MUI TextField and my objective is to input JSON text. However, I encountered an issue where pressing the tab key causes the component to lose focus and shift to the next element on the page. Is there a way to prevent the ta ...

using javascript to animate multiple div elements

In my current project, I am harnessing the power of Javascript to incorporate some eye-catching animation effects on a rectangle. Once the animation is complete, I have set the box to disappear from view. Check out the code snippet below for more details: ...

What is the best way to combine HTML, JavaScript, and CSS in a PHP function file for optimal integration?

how can I properly integrate HTML, JavaScript, and CSS code into a PHP format to place in the function.php file? I have created animations using HTML, JavaScript, and CSS formats, but when trying to add them to a PHP file like function.php, I am facing dif ...

How can I retrieve the user input from an editable bootstrap component?

I am currently using the bootstrap-editable feature from the bootstrap3 library. I have a situation where I want to retrieve the value of an input tag. For example, if I click on username and change it to myname, how can I then access the value myname? ...

Refreshing select2 dropdown options dynamically with AJAX updates

I have a select dropdown for locations that is initialized using select2 on page load. I am looking to dynamically update the data in the dropdown at regular intervals using ajax calls. However, when I attempt to update the data in select2, the dropdown ...

Utilize regex to substitute the first instance of a character within a word that is not at the start

For instance: When "initial" becomes "ineetial", "alumni" becomes "alumnee", and "illuminati" becomes "illumeenati." Yet, by employing this code: str = str.replaceAll("(\\w+)i(.?)", "$1ee$2"); I consistently receive 'initeeal', &apo ...

Creating a customized SelectField component for Material-UI v1.0.0-alpha.21 with a fix for the Menu anchorEl problem

Currently, Material-UI v1.0.0 does not have a selectField implemented yet so I am attempting to create my own version using TextField, Menu, and MenuItem Components. Below is the code for my custom selectField: export default class SelectField extends Rea ...

Is there a way to check if a user has previously visited a URL and automatically redirect them back to that page if they have

I'm looking for a way to detect if a user has already opened a specific link or URL in their browser tab. Is it possible to redirect the link to an active tab if the URL is already open? For example, if a user clicks on a link and JS code opens a new ...

I am encountering an issue where I keep receiving an error message stating that Selenium cannot be located

Traceback (most recent call last): File "c:\Users\user\OneDrive\Documents\GitHub\Python\GPUBuyer\Airis Tech Bot.py", line 1, in <module> from selenium import webdriver as wd ModuleNotFoundError: ...

Creating a custom inline style button in Froala Editor

As a UX Designer with experience in frontend development, I am currently working on an online editor proof of concept for a usability study. One of the key findings so far is that users are requesting a button that can automatically apply the corporate fon ...