Issue with Selenium WebDriver failing to locate the desired element using the nodePath/text() strategy

I'm currently attempting to extract a specific text element from a web application. Unfortunately, I am unable to provide the HTML code, but the relevant portion resembles the following structure:

<div id ="user">
    <div class="large columns">         
                <label for="object.disabled" class="readOnlyClass  hide-label">
                    Text1
                </label>
                <div id="123x435q" class="readOnlyClass  hide">
                    <input class="disabled-field" id="123x435qs" type="text" value="07/20/2017" disabled="">
                </div>
    <div class="class1">
      <div>Text2</div>
    "

    07/10/2017
    "
    </div>
</div>

My attempt involves using element.getText().trim() with xpath of //*[@id='user']/div[2]

The result I obtain is: Text2 07/10/2017

My goal is to isolate just the text '07/10/2017'. Therefore, I experiment with the xpath of //*[@id='user']/div[2]/text()[2]

Running this command in Chrome console: $x(//*[@id='user']/div[2]/text()[2]) yields the expected '07/10/2017' result. However, executing it via Selenium returns: Text1.

This leads me to two inquiries: 1. Why does path/text() return Text1 in Selenium but not in Chrome console? 2. How can I refine the element retrieval to solely retrieve the date text within the class1 div node without capturing the Text2 as well?

Answer №1

1 - Selenium does not have direct support for TextNodes. You can retrieve the text 'Text1' from the 'Label' element, but to access 'Text2' which is a TextNode inside a 'Div' element, you will need to use alternative methods.

2 - Here is an example of how you can retrieve the date text using different approaches:

String outerText = driver.findElement(by.cssSelector("#user > div.class1")).getText();
String innerText = driver.findElement(by.cssSelector("#user > div.class1 > div")).getText();
String dateText = outerText.replace(innerText,"");

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 creating a new json node with a Java library such as Jayway JsonPath

Here is a JSON string: {"store" : { "book" : [ { "category" : "reference", "author" : "Nigel Rees", "title" : "Sayings of the Century", "display-price" : 8.95 }, { "category" : "fiction", ...

Randomly position a div within a grid

I am currently working on creating a grid that spans 100% of the width of the browser window. Firstly, I am unsure about how to approach this grid creation, and secondly, I would like to have a div randomly positioned within the grid, filling the space onl ...

Can enums of the same type from different classes in Java be converted generically?

Dealing with an external model that consists of roughly 20 classes, I find myself in a situation where I need to work with these classes without the ability to modify them (e.g. FirstExtClass, SecondExtClass). Each of these classes contains an enum called ...

"Extracting Text from Images: Convert Images to Text using Optical Character Recognition

While referencing a live Codepen demonstration available at this link, the application functions correctly when the "choose file" button is clicked to upload an image from the local computer and extract text from it. However, my question is how can I uploa ...

Struggling to modify the background color of carousel captions using BootStrap4

I'm having trouble changing the background color of a basic carousel-caption element. Upon inspecting in the debugger, I keep getting the warning "Invalid property value," but I can't pinpoint the reason why. I find it odd that the 'backgr ...

Effective design for a Java project incorporating database functionality

I am currently experiencing database connection issues in my project which is built using Struts. I am struggling to understand how to effectively manage my database connections. It is crucial for my site to perform well, especially since it is expected to ...

Tips for altering the location header after a redirect from a proxied server in nginx

My Java application is running on a Jetty server with a Java REST web service backend and an AngularJS frontend. When the JSESSIONID cookie expires, Nginx responds with a 302 redirect for API calls. For instance, if I try to access http://www.example.com/r ...

Difficulty with animated presentations

As a developer with limited CSS experience, I am trying to create a CSS3 slideshow using only two images. After discovering some interesting code for this purpose, which can be found here, I decided to make a small change to the original code (specifically ...

Removing Swipe Transition with CSS

Currently working on customizing a Wordpress Template and aiming to implement specific effects using CSS. Take a look at the link: I am trying to remove the Swipe Effect that occurs when hovering over the pictures, but I am struggling to identify the cor ...

Setting up Selenium WebDriver with Maven and configuring the initial pom.xml

Embarking on my journey with Selenium WebDriver, I quickly discovered the importance of Maven and pom.xml files. However, I'm unsure about what these files entail and how to create them. Can someone provide insight into their significance? I would al ...

An issue arises with the Spring Boot CsvMapper bean when attempting to return a ResponseEntity that was generated using a Map

Within my Spring Boot application, there is a CsvMapper that has been injected into a Service class. This particular CsvMapper is defined as a bean within a Config file. Whenever I attempt to return a ResponseEntity in my Controller by passing in a HashMa ...

Is there a method in Selenium to open a new URL in the same tab while also being able to modify the URL in some way?

Looking for ways to open a new URL in the same tab using Selenium, but with the need to modify the URL somehow. What are some possible solutions? Considering navigating through keys, however the issue lies in the fact that the URL cannot be inspected. ...

Comparing results with ResultSet in an IF Statement in JAVA

In my current code snippet, I am asking the user to input depID to update a department. However, even though I have set it up to display either "Entry Saved" or "Department doesn't exist" based on an IF statement, it always goes directly to the else b ...

What causes the slight shifting of on-screen elements when the doctype is removed from an HTML document?

During the process of converting from HTML4 to HTML5, I compared my html4 and html5 pages and noticed that removing deprecated elements after the doctype declaration was causing slight movements in on-screen elements. For example, in the code snippet below ...

Generating new tr elements and storing them using JavaScript

I'm relatively new to coding, especially when it comes to JS. I am currently working on a program that is designed to calculate input values and display the results (along with the date) in a table. Every time there is a new input, a new entry should ...

Ways to prevent flex from expanding all the way to 100% width?

My section has two columns centered in flexbox, but the container doesn't wrap around them as expected and instead fills the whole page. How can I make the section wrap around the text with a padding of 1rem? body { background-color: #444; col ...

How to extract and print content within a div element using Selenium and Python

Looking to extract data from a website that has information nested under a specific div tag. Here is an example: <div> id="searchResults" class="multiple-view-elements" <span>name</name> <span>info</name> <span>info< ...

Tips for utilizing selenium storeValue in a separate window

I'm in need of assistance with creating a Selenium test case that includes the following steps: 1. Navigate to 2. Log in to the site 3. Select specific content and save it to a variable using selenium storeValue 4. Go to another site 5. Log in to t ...

Adjust the alignment of tooltip text to the left in Bootstrap 4

Using Bootstrap tooltip with HTML and looking to customize it. <span rel="tooltip" data-toggle="tooltip" data-trigger="hover" data-placement="right" data-html="true" data-title="{$some_html}"&g ...

Creating a dynamic 3x3 layout using flexbox: a step-by-step guide

Is it possible to dynamically create a layout in Next.js with 3 columns of 3 rows using flexbox, React Bootstrap, or CSS only? https://i.sstatic.net/tTinS.jpg ...