Tips for confirming text within a CSS class using Selenium

I am currently utilizing Selenium for automating a test that involves creating an app. The process entails filling out various fields on a webpage and then submitting the form. After submission, a new page appears with an alert message indicating either success or failure. The issue I am encountering is that the alert message is structured within a CSS class as shown below:

<div class="alert-box notice">
     Successfully created application
    <a href="" class="close">×</a>
</div>

My sole objective is to confirm the presence of the text "Successfully created application" without needing to alter any content.

Answer №1

It's important to identify the specific language you are using, as every language differs in syntax and functionality. In order to retrieve the text within a particular div element, you can utilize the following method:

Example scenario:

driver.findElement(By.cssSelector(".alert-box.notice")).getText()

Answer №2

After consulting with colleagues and conducting extensive online research, I finally arrived at a solution.

Initially, I attempted to extract the text based on Nathan's recommendation, but opted to save the output as a string.

String successMessage = driver.findElement(By.className("alert-box notice")).getText();
assertEquals(true, successMessage.contains("Successfully"));

However, I encountered recurring errors like "org.openqa.selenium.InvalidSelectorException: The given selector alert-box notice is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Compound class names not permitted."

Given that I couldn't change the class name, I decided to use xpath instead:

String successMessage = driver.findElement(By.xpath("html/body/div/div[2]/div/div/div/div[3]/div")).getText();
assertEquals(true, successMessage.contains("Successfully"));

This approach worked flawlessly, allowing me to successfully verify the alert message.

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

Establish a Zebra DataList Chart

My first task is to connect a Table from the database so that each row is editable. After some research, I settled on using the DataList due to its <EditItemTemplate> property, which the repeater lacks. To create a Zebra table, I crafted a CSS class ...

Leverage CSS to create a hover effect on one element that triggers a change in another

How can I use pure CSS to make an image appear when hovering over text? HTML <h1 id="hover">Hover over me</h1> <div id="squash"> <img src="http://askflorine.com/wp-content/uploads/2013/10/temp_quash.jpg" width="100%"> </div&g ...

Convert HTML content to a PDF file with Java

Welcome to the community! My project involves extracting data from a website containing information on various chemical substances and converting it into a PDF while preserving the original HTML formatting, including CSS styles. For example, here is a li ...

Stop flex items from expanding larger than their sibling elements

Is there a way to prevent a flex item from growing bigger than its siblings? Check out the example on CodeSandbox: LINK Here is the sample code: <div class='wrapper'> <div class='box one'></div> <div class ...

Dynamic <figcaption> that adjusts to different screen sizes with smooth animations

My first WordPress theme is almost complete and I'm excited to share it: The responsive image grid on the homepage is giving me some trouble. When I shrink the browser window, the images don't scale properly. The grid works fine without the JS ( ...

Center two lines of text in a div in a vertical fashion

After going through several articles on this topic, I've picked up some tricks for vertical alignment like using line-height for a single line of text, display:table for multiple divs, and Flexbox in CSS3. However, I'm still struggling to align t ...

Tips for creating responsive Math Latex

Check out this link to my website page. Also, take a look at this other link. While using Bootstrap, I've noticed that all content is responsive except for the equations in the first link and the Matrix multiplication example in the second link towar ...

Maintain scrolling at the bottom with React.js

Is there a way to make a div element increase in height through an animation without extending beyond the viewable area, causing the window to automatically scroll down as the div expands? I am looking for a solution that will keep the scroll position lock ...

Struggling to automate the drag and drop functionality on IE11 with Selenium WebDriver

Trying to automate drag and drop functionality in IE11 using Selenium Web Driver in Java. Success in Chrome, but facing challenges in IE. Here is how the dragging and dropping process is being done: Actions builder = new Actions(driver); builder.clickAnd ...

Input Fields Will Not Resize Correctly in Some Browsers

When resizing a browser, the input fields do not resize properly and end up overlapping each other in the middle, causing distortion before the media script adjusts everything to 100%. The width of the text area set to 100% does not align with the forms. F ...

Exploring the issue of varying site header widths in WordPress: The mystery behind my unfinished author page

Why is there a difference in site header width between the author page and other pages? It seems incomplete on the author page while it's complete on other pages. What am I missing? <header id="site-header" role="banner&q ...

easy method for creating a hyperlink that triggers a "download" pop-up box

Is there a simple and efficient way to have some of my links trigger the 'save file as' prompt (similar to right-clicking) immediately after they are clicked for the first time? ...

What could be causing my website to function flawlessly in Firefox but not in Chrome or Internet Explorer?

Hey everyone, I'm feeling lost and have been comparing files using DiffNow. I don't have any code to offer because I'm unsure of where to even begin. The website is coded in php/html/mysql/jquery. Here are my main concerns: -Animations are ...

Do I have to implement a column grid for creating responsive websites?

I'm feeling a bit uncertain about the necessity of a column grid for creating a responsive website. For instance, I plan to kick off with the Bones WordPress theme. My intention is to utilize the SCSS setup provided in this theme. The grid it offers i ...

Tips on identifying the problematic driver in an ITestResult listener when multiple browsers are being utilized within the same test method

When running a test method with multiple browsers or drivers, how can we determine where a failure occurred (i.e., in which browser)? For instance, let's say we have a test where we launch two browsers, log in with different credentials, and perform ...

Using jQuery, generate a div element and display it in full screen mode

Is it possible to dynamically create a full-screen div using jQuery directly from JavaScript without the need to first define it in the HTML and then make it visible with jQuery or CSS? UPDATE After some troubleshooting, I found a solution to my issue: ...

In configuring the print settings, I specified margins to ensure proper formatting. However, I noticed that the margin adjustment only applies to the first page. I need

I have a method that retrieves margin top value from the backend. It works perfectly on the first page of print, but on the second page, the margin top space is missing. initializePrintingSettings() { this.printService.fetchPrintSettings().subscribe(respon ...

I am attempting to extract video information from a YouTube channel using Selenium, but unfortunately, I am only getting the details of one video repeated multiple times as my output

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome() driver.get('https://www.youtube.com/channel/UC9B_ywLK2Vne4mtmGgWZklg/videos') videos = driver ...

The absolute positioning is causing the <UL> <LI> elements to malfunction

Hello I have encountered an issue with the bootstrap menu that I am using. The first link is not functioning properly due to the position: absolute; attribute of the <li> element. Any suggestions on how to resolve this would be greatly appreciated. I ...

Ever thought about harnessing the power of SassDoc to create detailed documentation for your sass

After experimenting with sassdoc for documenting our sass mixins and functions, I realized that there doesn't seem to be a straightforward way to generate documentation for our sass variables. Specifically, I am looking for a solution that can output ...