Using Selenium and Java, one can extract the "Effective Date" text from the text node within the th node

Can someone help me with extracting the text Effective Date from the image provided below? What would be the correct way to create an xpath for this?

Below is the snapshot of the HTML:

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

Answer №1

There is a possibility, but I recommend using CSS for this task.

If you choose XPath:

String text = driver.findElement(By.xpath(
                   "//*[@class='targetDiv']/.//span[contains(@id,'specificLabel')]"))
              .getAttribute("innerHTML");

If you prefer CSS:

String text = driver.findElement(By.cssSelector("#DivId span.desiredLabel"))
              .getAttribute("innerHTML");

Answer №2

When working with Selenium, my preferred locator is typically the element's id. However, in this particular situation, the target element does not have an id attribute. In such cases, I would look for the name property as an alternative, but unfortunately, it is also not present. As a last resort, I turn to using xpath, even though it may be slower. Nevertheless, xpath is versatile and can locate almost any element. In your scenario, the following xpath locator could be used:

String elementLocator = "//span[@id='rightmandatory']/parent::th";
String text = driver.findElement(By.xpath(elementLocator)).getText();

If the getText() method retrieves the text from the span element, you can consider using

.getAttribute("innerHTML")
as an alternative. While there are other locators available, you specifically asked for the xpath option. I hope this information proves helpful.

Answer №3

The Effective Date text is embedded in a text node. To extract this text, you will need to employ the use of WebDriverWait for the method visibilityOfElementLocated() and you can opt for either of these Locator Strategies:

  • cssSelector:

    System.out.println(((JavascriptExecutor)driver).executeScript("return arguments[0].lastChild.textContent;", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table.data_form.label tbody > tr > th")))).toString());
    
  • xpath:

    System.out.println(((JavascriptExecutor)driver).executeScript("return arguments[0].lastChild.textContent;", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@class='data_form label']//tbody/tr/th")))).toString());
    

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

Issues with Node.js and AJAX

I am having trouble with my ajax request to retrieve data from node.js. The interaction between the two seems off, but I can't pinpoint where the issue lies. Below is the snippet of my ajax get request: $.get('/notificationsNumber', ...

What is the best way to generate a search link after a user has chosen their search criteria on a webpage?

In my search.html file, I have set up a form where users can input their search criteria and click the search button to find information within a database of 1000 records. The HTML part is complete, but I am unsure how to create the action link for the for ...

How does the memory of a Java Web Container react to an abundance of simultaneous user sessions?

Today, a question arose while I was at work: What happens when I have numerous users accessing my website concurrently, each with their own data stored in user sessions, and my JVM runs out of memory due to its limitations? Despite searching through the ...

Encountering issues with resolving dependency tree post updating node, specifically with node-sass dependency causing failure

Following the update to the latest version of Node.js, I encountered error messages when attempting to use npm audit fix --force. It appears that resolving dependency tree issues is proving to be difficult. Despite extensive research and trying various s ...

What is the process of invoking an external JavaScript function in Angular 5?

I recently downloaded a theme from this source. I need to specify script and CSS in the index.html file. The body section of index.html looks like this: <body> <app-root></app-root> <script type="text/javascript" src="./assets/js ...

The native javascript modal fails to appear

I'm attempting to implement the functionality from this Codepen demo into my project. I've copied over the HTML, CSS, and JavaScript code: <!DOCTYPE HTML> <html> <head> <script> var dialog = docume ...

What steps can I take to make sure a particular node is successfully loaded and ready for use using JavaScript/jQuery?

When making a reservation at a hotel using the CJS Chrome extension, I am attempting to extract information about available rooms and rates both when the page loads and when the user changes dates. My current method involves injecting JavaScript into the p ...

Slow CSS :hover animations in the most recent release of Chrome

I recently upgraded my browser from chromium version 67 to the latest Chrome version 79. However, I noticed that after the upgrade, the CSS transitions on my website have become very laggy and unresponsive. Surprisingly, everything works perfectly fine on ...

What provides quicker performance in AngularJS: a directive or one-time binding?

I need to display a static array on a webpage without Angular watching its value. With that requirement in mind, my question is: Which method is faster: using a directive to fetch a scope variable and create an HTML element with this variable as a hard-co ...

Error: Attempting to access the 'clipboard' property on an undefined object results in a TypeError when invoking this.$q.electron.clipboard

I'm currently working on incorporating copy to clipboard functionality into my app using Electron. This is the command I am using: methods: { copyToClipboard () { if (process.env.MODE === 'electron') { this.$q.electro ...

Automating the process of clicking on each link and opening them in new tabs with Selenium Ruby

I am looking to modify my code in order to open all clicked links in new browser tabs once the page loads using driver.get "http://www.example.com". Despite receiving answers from previous questions, the solutions provided have not met my expectations. Bel ...

Creating a JSON file with a string variable is a simple task that involves defining the

I have a string variable that I need to use in creating a JSON file. The JSON file should contain the content of the variable named "result". Any assistance with this task will be greatly appreciated! Thank you in advance! @TargetApi(Build.VERSION_CODE ...

tracking scroll position within div on main page

I have a div tag enclosed within a content tag due to the implementation of a masterpage containing the forms and body tags. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"> <div id="xxx" style="overflow:s ...

A guide on sending JavaScript variables to PHP using AJAX

I've been facing an issue while trying to pass the selected month from a select list in HTML using Ajax. Every time I select a month, it doesn't show up as expected. Initially, there's a placeholder "nothing" displayed due to the if-else con ...

Ensure the detection of 404 error status using jQuery

My current approach involves using this code snippet to retrieve data from Twitter through its API: $.ajax({ url : apiUrl, cache : false, crossDomain: true, dataType: "jsonp", success : f ...

Exploring the World of jQuery Caching and Navigating Through Objects

I'm interested in learning more about jQuery caching and how it can enhance performance. Can you explain how to properly utilize this technique? From what I understand, when using a jQuery selector, you're essentially searching the DOM to create ...

Ways to correct the issue of double animation effect while utilizing the $(window).scroll function?

Currently, I am facing an issue where the elements in viewport trigger the animation twice when the user scrolls. This strange behavior only occurs when the page is loaded at the very top and then scrolled down. If the page is loaded anywhere else, everyth ...

Leveraging jQuery to detect an AJAX load that does not involve the use of jQuery's AJAX method

Hi all, I have a challenging issue that I'm struggling with in terms of jQuery/JavaScript. I am fetching data through standard AJAX without using any frameworks like jQuery. Now, the dilemma is that once the page has loaded, I need to implement a jQu ...

Under specific circumstances, it is not possible to reset a property in vue.js

In Vue.js, I have developed a 'mini-game' that allows players to 'fight'. After someone 'dies', the game declares the winner and prompts if you want to play again. However, I am facing an issue where resetting the health of bo ...

What methods are available to gradually increase a counter until it reaches a specific number by a designated date?

I have a unique idea for my website - I want to create a special counter that gradually increases to a specific number, but does so over the course of an entire year. Imagine starting at 0 and aiming to reach 35340340 after exactly one year has passed. It ...