Utilizing CSS class names for Xpath or extracting locators with CSS styling

I am currently working on a webpage that features a series of radio buttons, each accompanied by a help icon. When this icon is clicked, it triggers a popup with helpful information. I am in the process of using Selenium WebDriver to extract this help text. My goal is to specifically obtain the text "HELP TEXT 2". I would greatly appreciate assistance regarding how to construct an XPath or locate the help text using CSS. If there are simpler methods aside from XPath or CSS that you can suggest, please share them with me. For this task, I am utilizing Firefox and implementing Java bindings for Selenium.

<div class="a">
<div class="b" tabindex="0">
<input tabIndex="0" id="1">
<div class="c">
<label for="1"> apple </label>
<div class="d">
<span>Help</span>
</div>
<div class="f" role = "doc">
<div class="g">
<div class="h">
<button type="button" class="close"></button>
</div>
<div class="i" tabindex="0">
HELP TEXT 1
</div>
</div>
</div>
</div>
</div>
</div>

<div class="a">
<div class="b" tabindex="0">
<input tabIndex="0" id="2">
<div class="c">
<label for="2"> orange </label>
<div class="d">
<span>Help</span>
</div>
<div class="f" role = "doc">
<div class="g">
<div class="h">
<button type="button" class="close"></button>
</div>
<div class="i" tabindex="0">
HELP TEXT 2
</div>
</div>
</div>
</div>
</div>
</div>

Answer №1

It seems to me that what you're looking for is not simply "HELP TEXT 2", as that would be too easy, but rather the help text linked to the button labeled "close". If my assumption is incorrect, feel free to disregard this response and remember to provide more details in your next inquiry.

If I'm on the right track here, you might need to use something like

//div[button[@class='close']]/following-sibling::div[1]
.

Answer №2

It appears that your HTML lacks unique identifiers for individual radio buttons. In this case, I recommend retrieving all help texts using the CSS selector ".i". This will return a list of help texts containing ["HELP TEXT 1", "HELP TEXT 2"]. You can then test all of them simultaneously in your test case.

Answer №3

To retrieve all Help Text elements, you can utilize the findElements() method as shown below:

List<WebElement> helpTextList = driver.findElements(By.xpath("//div[@class='i']"));

for(int index = 0; index < helpTextList.size(); index++){

     String helpText = helpTextList.get(index).getText();
     System.out.println(helpText);

}

Answer №4

If you want to retrieve the help text without hovering, consider using getAttribute("textContent").

Based on the xpath provided earlier, you can utilize this code snippet to extract the first instance of Help text:

String help_text1 = driver.findElement(By.xpath("//label[contains(text(),'apple')]/following-sibling::div[@role='doc']//div[@class='i']")).getAttribute("textContent");
System.out.println(help_text1);

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

Is it possible to have the cells in a grid adjust and fill the available space when the size of the grid changes?

Hey there! I'm currently working on creating a dynamic grid but I'm facing an issue with making the cells fill the space between them when the size of the grid changes. Within my function that generates the grid, I take in the size as a paramete ...

Navigating through and updating a doubly-linked list containing character values using a listIterator

I am working on traversing a Doubly-linked list of characters. The task is to replace every capital 'Y' in the list with a '!' and then, while iterating backwards, replace every 'M' with a blank space (' '). If more ...

Struts 1.3: Implementing a default Locale in a web application

In my Struts application, I am working with multiple i18n files. Currently, I switch between these files by adjusting the Global.LOCALE_KEY variable within the session. Is it possible to define a default locale for the entire application? Perhaps in the s ...

Integrating third-party libraries with GWT using JSNI

I am currently working on integrating Opentip into a GWT project. Since some of my widgets are loaded from Java rather than HTML, it seems like I will need to utilize JSNI in order to properly bind those tooltips. Here is the progress I have made so far: ...

Selenium with Python: Navigating through a lengthy list to pinpoint and select a specific link

In my current project, I am facing a challenge where I have a list of over 100 websites and I want to automate the process of navigating to each site using Selenium webdriver. As a beginner in programming, I apologize if my question is not clear enough. My ...

Improving the performance of System.arraycopy when working with multidimensional arrays

One common performance optimization technique involves replacing simple for loops with System.arraycopy. I have a couple of questions: At what point does using system.arraycopy become beneficial, considering it is a native method call? Are there any a ...

Disappearance of CSS borders when using jQuery scroll function

After implementing a fixed table header for an HTML table on my website, I encountered a minor issue. When scrolling down, the table header remains at the top as intended, but the CSS styling for borders within each <th> element disappears. The font ...

Can you explain how Java is translated?

Today, my curiosity lies in understanding the intricacies of the translation process specific to Java. While I am familiar with the general translation process, I am eager to delve deeper into how it specifically functions within the Java programming langu ...

Integrate Tailwind CSS into the bundled JavaScript file

Is there a way to integrate tailwind css into bundle js effectively? Take a look at this example involving vue 3 and tailwind 3 https://github.com/musashiM82/vue-webpack. Upon executing npm run build , it generates 3 files: app.js ABOUTPAGE.js app.6cba1 ...

Is there a method to achieve a similar effect as col-md and col-sm but for adjusting height instead?

I'm looking for a way to adjust the height based on the device size like how sm, md, and lg classes work. I'm designing a login form that looks great on cell phones, but appears too tall with excessive spacing when viewed on an iPad. There are la ...

Ensuring the clickable area of the button aligns perfectly with the button itself and adjusting the arrow position to be

Check out the sandbox here: https://codesandbox.io/s/vigilant-currying-h7c3r?file=/styles.css If you are looking for the classes .line and .arrow, they are what you need. When you create an event, notice how the arrows appear too low, with the clickable a ...

I am unable to arrange JButtons in a specific sequence within a JList

I'm attempting to create a board game by adding JButtons to a JList. Below is the code I've written: public class Board { public Board() { JList list = new JList(); list.setLayout(new GridLayout(10, 10)); list.setDragEnabled(true); ...

There was an issue while trying to create the 'entityManagerFactory' bean defined in the ServletContext resource, resulting in the inability to build the Hibernate Session

I have been struggling with this project for the past few days and I am feeling completely lost. Despite following numerous tutorials, I am unable to make any progress. Initially, I had a setup that seemed to work, but I later realized that it didn't ...

The CSS division is perplexingly dimensionless

As I work on finalizing a client's webpage, I encountered an issue with applying vertical padding to one of the div elements. Upon inspecting the element using Chrome's dev tools, I found that it had "NaN x NaN" dimensions. This prevented me from ...

Enhancing a @Subselect Entity with @Filter and @FilterDef by including parameters

My attempts to achieve the same goal mentioned here have been unsuccessful: How can I bind parameters to the SQL request inside a @Subselect annotation in Spring Boot? I am aiming to restrict the results of this subselect to objects in table2 that have a ...

Tips for positioning the footer in HTML and CSS

footer { background-color: #000000 } .footer-nav { list-style: none; } .footer-nav li { display: inline-block; margin: 15px; font-weight: 400; font-size: 80% } .social { list-style: none; } .social li { display ...

Looking for a digit located after a particular word that is not directly before the digit

I am having trouble implementing a pattern to search for a Zip Code within a string. The current implementation is not working as expected. Here is an example of the inputLine: Can you tell me the Zip Code for 90210? The pattern I am using is as follow ...

Tally utilizing the Advanced for Loop

I am facing an issue with the code snippet below. My objective is to count the occurrences of the letter 'e' in the String "abcdee". class Sample1 { String tiles; public Sample1 (String tiles) { this.tiles = tiles; } pu ...

Eliminating empty space within my container

I am facing an issue with a container that contains an image and a button on top of it. There seems to be some extra space created within the container on the right side which is taking up more space than necessary. The layout consists of three horizontal ...

Error encountered: AWS Lambda function unable to deserialize into POJO (Plain Old Java Object)

In my project, I am utilizing Spring Boot (version 2.26) in conjunction with AWS SDK for Java (version 1.11.761). Currently, I am encountering an issue where the @LambdaFunction fails to deserialize to my POJO, resulting in the following error: "com.fa ...