Navigating through text after a specified label with XPath

Currently exploring Selenium for testing purposes, I've run into some issues. Let's say I have the following:

<div class="itemize-row">                                              
  <p class="subText">
    <span class="item-label">Card Color:</span> Mandarin
    <span class="item-label">Colored Mug:</span> Red
  </p>
</div>  

Could someone assist me in retrieving the data "Red" using XPath or CSS?

Answer №1

When utilizing this XPath query,

//span[@class='item-label' and .='Colored Mug:']/following-sibling::text()[1]

The expected outcome will be the value `"Red"`.


While generally effective, it is important to note that in Selenium, text nodes cannot be directly targeted. XPath expressions must refer to "elements". – alecxe

To accommodate for Selenium's limitations, you can use the following modified XPath query:

substring-after(//span[@class='item-label' and .='Colored Mug:']/.., 'Colored Mug:')

This revised approach leverages the positioning of your desired content within the string value of the parent element, ensuring that "Red" will still be returned as requested.

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

Use JavaScript to change the CSS pseudo-class from :hover to :active for touch devices

Looking to eliminate hover effects on touch devices? While it's recommended to use a hover class for hover effects, instead of the hover pseudo-class, for easier removal later on, it can be a challenge if your site is already coded. However, there&apo ...

CSS: Navigation menu causing a delay in the display of a div

Hey there, I am a beginner in HTML & CSS and currently working on my first website. I'm facing an issue with my navigation bar causing a delay in the div below it when set to relative position. When I switch it to absolute positioning, the centering o ...

Using VBA and Selenium to Select Dates within an iframe

https://i.sstatic.net/0wi5j.png https://i.sstatic.net/Uy8Io.png I am facing challenges in selecting a date from a date selector within an iframe. Despite trying various methods like CSS, Xpath, ID, and Class selectors, I have been unsuccessful in selecti ...

Is there a specific JSON schema designed for the SurveyMonkey API?

Currently, I am using the SurveyMonkey API to write Java code for survey analysis. The API provides JSON data which I need to manipulate efficiently and safely in my code by generating specific Java classes. However, despite my efforts, I have been unable ...

Error caused by Gson Overflow

While attempting to convert a Json string into a Java object using the Gson library, I encountered a StackOverflowException. java.lang.StackOverflowError com.google.gson.internal.$Gson$Types.checkNotPrimitive($Gson$Types.java:431) com.google.gson ...

Create a resizable textarea within a flexbox container that allows for scrolling

Hey there! I'm struggling with a Flexbox Container that has three children. The first child contains a textarea, but it's overflowing beyond its parent element. Additionally, I want to make the content within child 2 and 3 scrollable. I've ...

What causes the difference in behavior between absolute positioning in <button> and <div>?

I am noticing that the code below is not positioning my span to the top-left corner of the button as I expected. Can anyone explain why? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> &l ...

Images with scratches visible in Chrome

This puzzle may seem easy to some, but it's been quite challenging for me. I've encountered a discrepancy in how Firefox and Chrome render images (specifically technical drawings). Despite my attempts to use image-rendering: -webkit-optimize-cont ...

Load a page and sprinkle some contents with a slide effect

I am brand new to jQuery and just starting out, so please excuse me if this question seems basic or silly. I would like the header and footer of my page to stay in place while the center content slides in from the right side when the page loads. This websi ...

Ways to shoot a reply to a native Android application

In my current project, I am working with a native Android app where users can create a profile to access specific features. Depending on the application's setup, users are either redirected to a URL in their browser for authentication or shown a login ...

Hide the div once it goes off screen, ensuring that the user stays in the same position on the page

On my website, I implemented an effect similar to the Airbnb homepage where there is a "How it Works" button that toggles a Div element pushing down the entire page. However, when the user scrolls to the bottom of the toggled div (#slideDown) and it disapp ...

Creating a CSS slideshow with z-index

Is it feasible to use CSS exclusively to design a carousel that switches the z-index of divs at set intervals (for example, every 5 seconds)? The divs would be arranged in layers on top of each other and as their z-index changes, they would move forwards ...

The lightbox is triggered by clicking on a different div to display its content

Great news - my lightbox is up and running! Each image on my website corresponds to a different organization, and clicking on the image opens a specific lightbox. My question is: how can I have a lightbox configured so that only the trigger image is displ ...

Breaking down strings according to ABNF structure using regular expressions

I have a string that adheres to a specific grammar. The string could look like this: String query = query1 (param1,param2), query2, query3, query4 (param1); Each query and param in the string can be any combination of characters and digits. We are look ...

Click on a link with partial text to choose a specific value using Selenium

Imagine this scenario: You search Google using the keyword 'Selenium' and click on the first link. Take a look at the code snippet below that I've written: `WebDriver driver = new FirefoxDriver(); driver.get("https://www.google.com/"); d ...

ReactJS Navbar component experiencing CSS hover bug

While developing a navbar component in React, I encountered an unexpected issue - the hover effect on my navigation links suddenly stopped working. Strangely enough, the cursor: pointer functionality still operates on my logo but not on the nav links durin ...

Java offers the ability to calculate exponential expressions with ease, utilizing the power of

Curious about the proper way to express x^(1/3)? Here is my implementation that gives the correct result of 2 for 8^(1/3). Are there any alternative methods worth considering? int a = 8; System.out.println(Math.pow(8, 1/3.0)); // returns 2 Best regards, ...

What is the best permission to configure in order to prevent issues with the Security Manager when using URLs with https?

In a custom software application, we are required to retrieve content from specified URLs. Additionally, the client has requested the activation of the Tomcat Security Manager to enforce Java Policies that control the program's actions. However, we a ...

jQuery is working perfectly on every single page, with the exception of just one

Check out my code snippet here: http://jsfiddle.net/9cnGC/11/ <div id="callus"> <div class="def">111-1111</div> <div class="num1">222-2222</div> <div class="num2">333-3333</div> <div class="num3">444-4444< ...

Displaying an array value instead of a new value list in a React component

Situation - Initial number input in text field - 1 List of Items - 1 6 11 Upon removing 1 from the text field, the list becomes - New List Items - NaN NaN NaN Now, if you input 4 in the field. The updated List Items are - NaN NaN 4 9 14 Expected ...