How to use WebDriver to select and copy text within a div element

Looking to extract specific text from a DIV? Take a look at this sample DIV source:

<html>
<div class="roamingHostIdContainer ng-binding">
                                        Host ID: 3K9X-Q8LD-6AX6-3UGP-UL5B-YE3Z-UWCD-DGDU-AB8Y-FJD2-7W97-A63J-RVZA
                                    </div>
</html>

It seems like the div has excessive spaces. But, my main goal is to retrieve and copy the ID value.

I first approached my challenge through these inquiries: How to manipulate user selected text using webdriver?, then shifting to: How to move cursor in Selenium Webdriver

My initial thought was utilizing a javascript executor, but I am uncertain about its application. My strategy involved creating elements with just the "Host ID:" and "RVZA" text respectively. However, creating an element based solely on that text could pose issues as both elements would be identical.

If anyone can offer guidance or steer me in the right direction, I would greatly appreciate it.

Answer №1

Forget about Selenium for a moment - this is all about mastering the art of Java String manipulation.

String myContent = driver.findElement(By.className("roamingHostIdContainer")).getText();
int indexOfID = myContent.indexOf("ID:");
String extractedID = myContent.substring(indexOfID + 4).trim();

driver.findElement(some-other-area).sendKeys(extractedID);

Answer №2

An alternate method to achieve this is:

element.sendKeys(Keys.chord(Keys.CONTROL, "a"));
element.sendKeys(Keys.chord(Keys.CONTROL, "c"));
element2.sendKeys(Keys.chord(Keys.CONTROL, "v"));

This could be the solution you need.

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

"Ensuring Proper Validation for Forms with JavaScript: A Step-by-Step Guide

Here is a sample Javascript form: <form id="loginForm" name="loginForm" action="/create_session/" method="post">{% csrf_token %} <table border="0" cellpadding="0" cellspacing="0" id="id-form"> <tr> <th valign=" ...

Can you explain the functionality of express-async-handler?

Hello, I'm trying to understand the purpose of express-async-handler. I came across it in a repository I was exploring. According to the documentation, express-async-handler is a simple middleware designed to handle exceptions within asynchronous exp ...

Retrieving Array keys from PHP using jQuery

As a beginner in jQuery, I am eager to learn how to retrieve Array keys from a php file using jQuery. Currently, I have jQuery code set up to send input to file.php and execute a query on every keyup event. When file.php echoes the result, it looks somet ...

Implementing the 'keepAlive' feature in Axios with NodeJS

I've scoured through numerous sources of documentation, Stack Overflow threads, and various blog posts but I'm still unable to make the 'keepAlive' functionality work. What could I be overlooking? Here's my server setup: import ex ...

What is the best approach for incorporating multiple conditions in a React component?

I currently have a button labeled Description. When this button is clicked, the description is displayed. Now, I am looking to include a Read more/less option for the description. Unfortunately, using the below code, I am unable to see the button labeled ...

Tips on locating all documents with an array field that encompasses exactly two precise values

After executing this code, I noticed that it retrieves all documents where the authors field contains both specified values along with other values. Document authors = new Document("authors","firstValue") .append("authors", "secondValue"); MongoC ...

Spontaneously fluctuate image transparency

I'm in need of some assistance with an issue I'm facing. I have 5 images positioned using Bootstrap and I want to add a fade animation to them on page load. Below is the code snippet I've been using, which works fine, but I would like the im ...

Ways to obtain a referrer URL in Angular 4

Seeking a way to obtain the referrer URL in Angular 4. For instance, if my Angular website is example.com and it is visited from another PHP page like domaintwo.com/checkout.php, how can I detect the referring URL (domaintwo.com/checkout.php) on my Angul ...

Executing Selenium tests in parallel with TestNG and skipping or disabling specific tests based on parameters passed through Maven command line

We operate a multinational website where not all features are available in every country due to different languages. Currently, we have hundreds of tests built on the Java stack - including TestNG listeners, Selenium WebDriver (following the Page Object Mo ...

Main.js creation issue in AngularJS 2

After meticulously following the Angular JS2 TypeScript tutorial and setting up the correct paths, I encountered an issue when testing in the browser. The error message states that it cannot locate the main.js file. Even after starting npm, the console dis ...

Stop displaying AJAX requests in the console tab of Firebug, similar to how Twitter does it

I'm looking for a way to prevent my AJAX calls from being logged in Firebug's Console tab, similar to how Twitter does it. When using Twitter search, you'll see a live update feed showing "5 tweets since you searched." Twitter sends periodic ...

triggering a function from a child component in React

I am facing a challenge in calling a function from the parent component that is stored in a child component. I understand how to do it from child to parent using props, but unsure about how to achieve it from parent to child. In the example below, you can ...

There appears to be an unspecified parameter in the controller related to the ng

After fetching data from an API, I use it to populate a form with cascading select fields. I have two functions in place to filter the array based on the selected options, but I am running into an issue where I cannot access properties from ng-model. For ...

Arranging the JSON response retrieved by AJAX

Hey there, I'm currently faced with an ajax call that retrieves a json string containing the following structure: "{"d": [ {"path":"/","e_type":"d ","text":"/"}, {"path":"//SQL","e_type":"d ","text":"//SQL"}, {"path":"//SQ ...

JQuery UI Autocomplete - Issue with loading data object

I've been attempting to implement autocomplete with JQuery UI, but I'm encountering difficulties when passing in a label & value object. var individuals = []; var test = new Array(); var dataObject = jQuery.parseJSON(data) ...

Receiving a 405 error when making an API call - could the routing be misconfigured? (Using NextJS and Typescript)

Hey there, I've been working on implementing a custom email signup form that interfaces with the Beehiiv newsletter API. If you're interested, you can take a look at their API documentation here: Beehiiv API Docs Currently, my web application i ...

Is it possible to open a new window using "onclick" event handler in HTML?

Currently, I am facing an issue with a storyline file that opens in a new window when clicked on an event in my index file. The code snippet for this scenario is as follows: <div class="ease col-6 filterDiv interactives"> <img class="display ...

AngularJs - Server encountering issue: "XMLHttpRequest is unable to load the specified 'URL'. Preflight response is invalid due to redirection

Can you figure out why the server is returning 'undefined' and 'XMLHttpRequest cannot load the "URL" Response for preflight is invalid (redirect)'? The app is supposed to send document details to the server through a normal post servic ...

Tips for extracting values from a PHP array using JavaScript

Assuming we have a PHP array named $decoded: $decoded = array( 'method' => 'getFile', 'number' => '12345' ); The data from this array will be passed to a JavaScript function called get(params). funct ...

How can the values of an array be adjusted?

Currently I am working on an inventory script to display the player's inventory using an array. I am having trouble setting a .amount property as I am encountering undefined errors when attempting to set them. Unfortunately, I am unable to utilize set ...