What is the best way to capture the dynamic text "3204255" from the html content using Selenium?

My goal is to extract a number from a specific div element during the execution of my test cases. I am able to locate the element, but I am struggling to retrieve the number and print it to the console.

This is what I have tried:

WebElement webelement = driver.findElement(By.xpath("//div[contains(@class,'responsive now') and text()='Application number']"));
webelement.getText();

and

driver.findElement(By.xpath("//div[@class='responsive-show']")).getAttribute("value");

While there are no issues with the locators, the number is not being displayed.

<td> 
  <div class="responsive-show">Application number</div>
  "3204255" 
  <input data val="true" data-val-number="The field ExaminationNumber must be a number." data-val-required="The ExaminationNumber field is required." id="ActiveExamModeIs_0__ExaminationNumber" name="ActiveExamMode[0].ExaminationNumber" type="hidden" value="3204255"> 
  <input data -val="true" data-val-number="The field ExaminationEligibilityExamTypeId must be a number" data-val-required="The ExaminationEligibilityExamTypeId fiels is required." type="hidden" value="1">
</td>

Answer №1

According to the HTML provided, if you want to extract the number 3204255, you will need to implement a WebDriverWait for the specific element to become visible. Here is a solution using the executeScript() method:

  • Solution in Java:

    WebElement myElement = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@class='table table-hover']//tbody/tr/td")));
    String myText = ((JavascriptExecutor)driver).executeScript('return arguments[0].childNodes[2].textContent;', myElement).toString();
    

Answer №2

To put it simply: there isn't an easy way to extract the text value using basic Selenium functions without resorting to the use of "JavascriptExecutor"

An alternative approach is to retrieve the entire text from the <td> element using regular selenium methods and then utilize the String function substring() to isolate your Application number value which is enclosed in quotes ".

Sample Code:

    String input = driver.findElement(By.xpath("//table[@class='table table-hover']/tbody/tr/td")).getAttribute("innerText");
    input = input.substring(input.indexOf("\"")+1, input.lastIndexOf("\""));
    System.out.println("Extracted Value" + input);

Answer №3

text: 3204247 can't be found within a div tag, but it is located within <td>. Here's a suggestion to try:

WebElement webElement = driver.findElement(By.xpath("//table[@class='table table-hover']/tbody/tr/td"));
// You can refine this locator and also use the one below
// WebElement webElement=driver.findElement(By.xpath("//div[@class='responsive-show']/..")); using xpath axes
String value = webElement.getText(); // OR
// String value = webElement.getAttribute("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

Updating the progress state of MUI linear determinate diligently

I currently have a modal set up that handles some asynchronous logic for submitting data to a database. The component I am using, called LinearDeterminate, is designed using Material-UI. You can find more information about it here: MUI Progress import { u ...

A guide to troubleshooting CoffeeScript on the server using NodeJS

Below is the code I'm working with in Coffeescript, NodeJS PhantomJS, and SpookyJS. try Spooky = require 'spooky' mysql = require 'mysql' catch e console.log e Spooky = require 'node_modules/spooky/lib/spooky' ...

Issue with jqGrid when filtering small numerical values

Happy Holidays! I recently came across an issue while trying to filter small numbers using jqGrid. I am filtering numbers that can vary from 10 to 1, down to values as small as 10^(-8) or even smaller. The filtering works well for these numbers until they ...

Angularjs - Provider not recognized:

I'm encountering an issue that keeps popping up: Error: [$injector:unpr] Unknown provider: UsersServiceProvider <- UsersService I attempted to address this problem by adding ['UsersService' before my controller function, after reading a ...

Selenium is struggling to locate a CSS selector

While using Selenium, I encountered a NoSuchElementException error after successfully retrieving 9 entries from the website. It seems like the page content might not have had enough time to load properly, but I'm still unsure. The code I wrote was ba ...

Changing the color of placeholder text in MUI 5 TextField

Looking to customize the text color and placeholder text color in my MUI TextField component to be green https://i.sstatic.net/NZmsi.png The documentation doesn't provide clear instructions, so I attempted a solution that didn't work: <TextF ...

What methods are most effective when utilizing imports to bring in components?

Efficiency in Component Imports --Today, let's delve into the topic of efficiency when importing components. Let's compare 2 methods of importing components: Method 1: import { Accordion, Button, Modal } from 'react-bootstrap'; Meth ...

How can I prevent the jcall error from appearing while utilizing read_osm() to showcase a basemap in plot mode for tmap within R?

Encountering an issue when trying to display a basemap for a static map or a map in plot mode with the tmap package in R. Below is the code used and the error messages received. Any suggestions on what might be causing this problem? I've updated the ...

Generate an HTML dropdown menu using information retrieved from an AJAX call

My select list is not displaying, even though everything else renders to the view as expected. The issue seems to be with my if-else statement in setting the selected option based on an ajax value. Is there a workaround to achieve this without using if-els ...

Issue with event listener not functioning properly with dynamically created content using AJAX (only using vanilla JavaScript

I used pure javascript AJAX to dynamically load content into the "test" div. However, when I try to click on a child div at index 6, an alert box is not being displayed as expected. How can I fix the issue with the click event not working? The gets functi ...

Converting URL-esque information to JSON using JavaScript

Does anyone have a solution for converting an array of URL-like data into JSON format? For example, how can we convert the array ["a.b.c.d", "a.c.e.f", "a.b.c.g"] into the following JSON structure: items:{ text: "a", items:[ { ...

``Java ProcessBuilder encounters a fatal error when attempting to shut down

ProcessBuilder pb; Process process; String command ="shutdown -s"; try { pb = new ProcessBuilder("cmd.exe", "/C", command) process = pb.start(); process.waitFor(); if (process.exitValue() == 0) { ...

Guide to achieving a powerful click similar to a mouse

I've been struggling to get an audio file to play automatically for the past three days, but so far I haven't had any luck. The solutions I've tried didn't work in my browser, even though they worked on CodePen. Can anyone help me make ...

Tips for enhancing the refresh rate of OpenLayer Vector layers with 200 elements

I am working on developing a timemap that combines the jquery-ui slider component and the OpenLayer library. I am utilizing the Vector layer of OpenLayers with Rules style for this project. map = new OpenLayers.Map('map', {maxResolution:'a ...

Transform the post data into a JSON string within the controller

Hello everyone, I have a sample table that I want to share: <table class="table table-bordered" width="100%" cellspacing="0" id="tableID"> <thead> <tr> <th>A</th> <th>B</th> <th>C< ...

When I navigate using state.go in my controller, the Ionic slide menu fails to appear

Hello everyone, I am currently using Ionic to develop my application and have implemented a slide menu. However, I encountered an issue where the slide menu fails to work when changing views using "$state.go". How can I resolve this? The Router : "use st ...

Is it possible to export multiple named exports in a single set without changing how variables are called?

In my constants file I have: export const CONSTANT1 = 'CONSTANT1'; export const CONSTANT2 = 'CONSTANT2'; export const CONSTANT3 = 'CONSTANT3'; export const CONSTANT4 = 'CONSTANT4'; export const CONSTANT5 = 'CONS ...

How can numbers be translated into letters?

I have a new security feature for my system where users are required to enter obscure user IDs on their phones. To achieve this, I want to encode the IDs in such a way that guessing them becomes challenging. My plan is to convert the IDs into base-23 numbe ...

The list of items in a React Bootstrap Dropdown does not refresh when the state is changed

Trying to implement a dropdown with a list of checkboxes using react-bootstrap. The toggle event on the checkboxes works, but I'm facing an issue where the component is not re-rendered and the checkbox does not reflect the change. Check out the code ...

Contrasting the functionality of multidimensional arrays and implementing a property to each individual element, extending to all levels of nested

Currently, I am tackling the permission aspect of my application. The scenario involves developing a Vue application that receives data from a Laravel backend created by another individual. My current challenge lies in determining whether a permission is c ...