Tips for referencing the second class when two classes share the same name in Selenium WebDriver

I am trying to retrieve the text from the second "109-top-dark-grey-block ng-binding" class, which is currently null but will contain some text in the future. I have attempted using tabIndex and nth-child selectors, but neither have been successful. "

<div class="122-top-section-btm-half">
    <div class="108-top-grey-m12x3"></div>
    <div class="109-top-dark-grey-block ng-binding">ab ab xyz</div>
</div>

" "

<div class="d122-top-section-btm-half">
    <div class="108-top-grey-m12x4"></div>
    <div class="109-top-dark-grey-block ng-binding"></div>

"

Answer №1

Update

In order to retrieve the text from the second div block, you can utilize nth-child with a CSS selector. I personally tested this selector using Chrome tools:

https://i.sstatic.net/FaBcZ.png

Therefore, in your Java code:

String elementText = driver.findElement(By.cssSelector(".d122-top-section-btm-half:nth-child(2) .ng-binding")).getText();

This should effectively target the desired element, as specified by the CSS specification for nth-child indexing, where the index starts at 1 (not 0) and represents the second child.

Old Answer

Given the HTML snippet you provided, a CSS selector would be suitable for selecting elements. You could use the following approach:

String elementText = driver.findElement(By.cssSelector(".d122-top-section-btm-half .109-top-dark-grey-block")).getText();

Alternatively, if your goal is to target the element containing ng-binding within the first div, you may prefer a cleaner solution:

String elementText = driver.findElement(By.cssSelector(".d122-top-section-btm-half .ng-binding")).getText();

Both methods will return the text content of the element. To further enhance your understanding, consider exploring the CSS Selectors Guide.

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

Tips for avoiding flickering in a background image when it is being changed

Utilizing JavaScript, I am setting a repeated background image from a canvas to a div in the following way: var img_canvas = document.createElement('canvas'); img_canvas.width = 16; img_canvas.height = 16; img_canvas.getContext('2d' ...

The Jquery css on my website is not functioning properly

After creating a website feature where clicking on a specific div triggers an onclick event processing a chat_generate function, I encountered some issues. This funciton should insert HTML for the chat into a designated .open_div based on the id generated ...

Adjust the path of an element as it decreases in size

Sorry for the weird title, but that's likely why I can't find the solution on my own. I'm experimenting with CSS animations and I want the form element to decrease in height from 100px -> 0, but I want it to collapse from the top to the ...

Running Java code on a virtual machine

My current challenge involves running a Docker command from Java code, specifically in a designated directory. After some research, I came across this resource which explains how to execute an external command in Java. public Process exec(String command, ...

"Enhance your webpage with a captivating opaque background image using Bootstrap

I'm new to exploring Bootstrap and I am currently experimenting with options for displaying content with a semi-transparent background image. Currently, I am using a "well" but I am open to other suggestions. I have managed to place the image inside t ...

Are your macOS devices not displaying the Scrollbar CSS buttons correctly?

I need help troubleshooting why my buttons are not appearing in the scrollbar on macOS. They function properly on Windows, so I suspect there may be a typo or error in my code. Can anyone take a look and provide some insight? ::-webkit-scrollbar { wid ...

A dynamic Java web framework that combines RESTful JSON services with the power of HTML5 and jQuery AJAX functionality

As we approach the year 2013, the era of HTML5, jQuery has solidified itself as the go-to standard for web Javascript development. Back in 2010, this link was quite popular: I am searching for a Java web framework that can expose domain classes through R ...

Java: techniques for parsing this JSON

In my quest to extract information from a JSON object, I am seeking the values of either tel or user_id. Take for instance this JSON data: { "status":"ok", "account":{ "0":{ "user_id":"2", "thirdparty_id":"200", "tel":"28 ...

Ensuring the Line Breaks in CSS and JavaScript to Easily Modify the Style

Is there a way to determine when a line will break so I can apply different styles? The design team needs 3 buttons in a grid (3 columns) with specific sizes. They want buttons with content that breaks onto the next line to have a border-radius of 13px, w ...

Is it possible to utilize multiple firefoxProfileTemplates simultaneously in Selenium or specify them from the selenium object instead of during server launch?

Currently, I am utilizing Selenium RC for conducting website testing and I am in need of using multiple proxies simultaneously. To do this, I utilize firefoxProfileTemplate when initiating the selenium server. However, I have encountered an issue where mul ...

Steps for positioning a RadioButtonList at the center of a table cell

How can I easily center a RadioButtonList? It used to be straightforward, but for some reason the below HTML is not working. Can you spot what's missing? <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></ ...

Creating a dynamic multi-item carousel with Materialize (CSS) cards using data from a loop - here's how!

Using a for loop, the following code generates a list of cards. These cards are intended to be displayed in a carousel with 4 cards visible at once, and a next arrow button allows users to navigate through the next set of 4 cards. Materialize cards have ...

"Enhance your images with dynamic captions using jQuery

Struggling to create an image caption that only appears when hovering over the specific image? Not sure how to select the correct object? The caption is working, but it's appearing for all images at once. I have attempted to target a single image wit ...

Make a portion of the title come alive on hover

Looking to add animation to a title that consists of two parts: "HARD" and "WARE", with the second part having a more transparent color. When hovering over the "HARD" word, the colors reverse correctly. However, when hovering over the "WARE" word, only on ...

Ways to manage screen orientation changes on Android without using onConfigChanged

Having just ventured into the world of Android coding, I know there must be a silly mistake lurking in my code. My goal is to create a TO DO LIST app (without using Listview) but I'm struggling with handling orientation changes in Android. The issue a ...

Placing buttons on top of a video within a div container

I am facing a challenge with implementing custom controls on a video embedded in my webpage. I have tried setting a div to absolute position for the buttons, but they are not clickable. Is there a way to achieve this without using jQuery? I need the butto ...

Navigation bar text fails to display

Just starting out and trying to create a basic navigation bar, but I can't seem to get the links to show up. All that displays are the logo image and the hamburger icon. How can I make the links visible on the nav bar? Appreciate any help! <!DOCTYP ...

Troubleshooting a CSS overflow problem in the main slider

While exploring the website , I encountered a minor issue. When I added "overflow: hidden" to the featured slider div, the images appeared stacked if JavaScript was enabled or the user's internet connection was slow. However, a new problem has arisen ...

Combining Versioning with BundleConfig.cs: A Comprehensive Guide

Encountering a recurring issue where changes made to CSS or Javascript files do not reflect in client browsers unless they manually refresh the page with ctrl+F5. This poses a challenge, especially in a school system with numerous users who may not be awar ...

Having trouble clicking a button using Python and Selenium that triggers the `openWindow` function

I am attempting to interact with a button using Python Selenium WebDriver (Chrome). Here is the HTML code of the button: <button type="button" class="button blue" onclick="openWindow(LINK_HERE, 'idpage6')">Like</button> (I had to re ...