Looking for a Selenium script to target the following elements with either CSS or XPath selectors

Here is some HTML code snippet:

<div class="top_message_container">
    <div style="margin-left: 544.5px; top: -100px;" class="top_msg_div"> point deleted</div>
 </div>

I need assistance in writing Selenium code using Java.

This is what I've tried so far:

WebElement webElement = driver.findElement(By.xpath(".//div[contains(.,'point deleted')]")); 
String ActualMsg= webElement .getText(); 
System.out.println(ActualMsg);

Unfortunately, it is displaying as empty/null.

Answer №1

Upon observation, I noticed that the XPATH you employed is able to match with two WebElements.

If the className value is unique, consider utilizing By.className instead.

WebElement element = driver.findElement(By.className("top_msg_div"));
String ActualMsg= element.getText(); 
System.out.println(ActualMsg);

In case the above method does not work, try using XPATH:

WebElement element = driver.findElement(By.xpath("//div/div[contains(.,'point deleted')]"));
String ActualMsg= element.getText(); 
System.out.println(ActualMsg);

Answer №2

Have you attempted this method?

String xpath = "//div[@class='top_message_container']/div";
WebElement element = driver.findElement(By.xpath(xpath));
String message = element.getText();
System.out.println(message);
This code successfully displays the output. view image description

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

Ways to prevent content from being centered within a table cell?

Seeking clarity on how to align list items within table cells using CSS. Click the link below for a visual representation of the issue: I am looking to have each item in a list not centered vertically within its table cell. To view an example illustratin ...

What is the best way to test speedy AJAX response times using Webdriver.io?

Currently, I am creating Cucumber.js tests using Webdriver.io. Everything seems to be going smoothly, but I'm encountering an issue with the mock server responding too quickly with AJAX. The "Loading..." message is not visible because the content load ...

Having trouble populating a read-only text box nested within an iframe using the Watir WebDriver

Having trouble filling a read-only text field inside an iframe? Check out the navigation steps below. The URL is "" Click on the "Monthly Price" button. A text box will appear on overlay. Having difficulty filling that text box? Here are some meth ...

Creating an arrow line with CSS styling can be achieved easily

In order to create a horizontal line with a breakdown in the middle that displays an arrow, I want to use only HTML and CSS without relying on bitmap images. If needed, Font Awesome can be used to achieve the desired result. It's important for me to h ...

Utilizing window.matchMedia in Javascript to retain user selections during page transitions

I am encountering difficulties with the prefers-color-scheme feature and the logic I am attempting to implement. Specifically, I have a toggle on my website that allows users to override their preferred color scheme (black or light mode). However, I am fac ...

Incorporating vertical-align:middle into complex divs

I am faced with the challenge of aligning three divs which contain different lengths of text vertically. After experimenting with the following code: #container{ height:200px; vertical-align:middle; display:table-cell; } #content{ height: ...

JQuery integration resulting in disappearance of Input-group-btn element

Allow me to explain my current project. I am in the process of developing a Modal that enables users to input a password There will be an option to toggle between showing or hiding the text in the password field using a button positioned on the right sid ...

Find today's date using Xpath

On my webpage, there is a row that shows the name, relation, and date. I am trying to search for a specific row based on certain values using Xpath. The code I have created is almost perfect, except for the last part which involves the date. I would like ...

Problems with the design in the Opera browser

My website logo is displaying incorrectly in Opera, despite working fine in Firefox and Chromium. I've been trying to troubleshoot this issue for the past few hours with no success. The ID of the logo element is 'Logo'. You can view the sit ...

Is the HTML5 video backup image misplaced?

Here is the HTML I created to showcase a video background with two fallback images for mobile and old browsers: <div> <video id="video_background" preload="auto" autoplay="true" loop="loop" muted="muted" volume="0"> <!--<source s ...

Access your Yahoo email account by logging in through Python Selenium WebDriver

I'm finding difficulties logging into my Yahoo email account using Selenium with Python. Here's the code I'm currently using: from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Firefox() driv ...

CSS or jQuery: Which is Better for Hiding/Showing a Div Within Another Div?

Show only class-A at the top of the page while hiding all other classes (x,x,c). Hide only class-A while showing all other classes (x,x,c). Is it possible to achieve this? <div class="x"> <div class="y"> <div class="z"&g ...

The issue with collapsible elements not functioning properly in an Angular application involving CSS and JS

My implementation of collapsible in a plain HTML page looks like this: <!DOCTYPE html> <html> <head> <title></title> <style> button.accordion { background-color: #777; color: white; cursor: pointer; p ...

Eliminate all elements marked with a particular class when the user clicks outside of a

I am currently building upon a project that I previously started here. Essentially, what I'm doing is dynamically generating tooltip popups that appear next to a link when it is clicked. However, I now need these tooltips to close when any click even ...

Using FluentWait with built-in ExpectedConditions: A step-by-step guide

Attempting to utilize ExpectedConditions with FluentWait in Selenium (Java) but encountering issues. The following code is not functioning as expected, failing to wait for the element to appear in the DOM. Any assistance on resolving this issue would be g ...

What is the procedure for assigning an element's background-color to match its class name?

Is there a way to use jQuery to make the background color of a span element match its class? $(function() { $("span").css("background-color") }); span { display: inline-block; width: 5px; height: 5px; border: solid #0a0a0a 1px; } <script src= ...

What is the best way to anchor an image to the bottom right corner while also adjusting its position as

Hey there! I have a challenge where I want to anchor an image at the bottom right corner of a webpage. Initially, it works perfectly using this CSS: .logo img{ position: absolute; bottom: 0; right: 0; } However, when I resize the window, the ...

Unable to perform drag and drop functionality in Selenium WebDriver using Java with either Chrome or Firefox

https://i.sstatic.net/hjYJC.pngI've been working on automating a form designer, and I'm facing an issue with dragging fields to the "Special fields" section. Despite trying various solutions found online, I have not been successful in moving the ...

Leveraging mdbvue within the SAFE Network, style elements are noticeable by their absence

I checked out this handy tutorial But what really caught my attention was this tutorial: After following along, I ended up with the result below: https://i.sstatic.net/asgZs.png It's clear that something is missing in terms of style. As a novice i ...

Tips for maintaining the fixed size of a table header and preventing it from resizing in width

My goal was to create a table using divs with fixed headers while scrolling vertically. However, I encountered an issue where the header width seemed to shrink and became misaligned with the rows. Even setting the width to 100% did not resolve this probl ...