Input text with JavaScript in Selenium

How to dynamically insert a value into a specific HTML element using JavaScript

HTML code snippet:

<div class="CodeMirror-code" style="">
<div style="position: relative;">
<div style="position: absolute; left: -52px;">
    <div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 18px; width: 12px;">1</div>
</div>
<pre>
    <span class="cm-word"></span>
</pre>
</div>
</div>

Issue: Inserting text into the span element

<span class="cm-word"></span>

Code snippet:

WebDriver driver = DriverFactory.getWebDriver()
WebElement webElement = driver.findElement(By.xpath("//div[@class='CodeMirror-code']//pre"))
JavascriptExecutor executor = ((driver) as JavascriptExecutor)
executor.executeScript("arguments[0].value='sample text';", webElement)

Observation: Upon execution, the text value is not inserted into the element and no errors are shown. Traditional setText method is not effective, hence the JavaScript approach is implemented.

This scenario pertains to a Salesforce application in console mode execution

Answer №1

To change the text inside an element, you can utilize the innerHTML property:

executor.executeScript("arguments[0].innerHTML = 'changing the content';", webElement)

Other options to consider:

  • textContent:
    arguments[0].textContent = 'changing the content'
  • innerText:
    arguments[0].innerText = 'changing the content'

Remember, the value property is specifically for user input in form fields.

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

Transferring state to a nested child component within a parent component's child

Currently, I am in the process of developing a KanBan application using ReactJS. My main goal is to propagate the state from a parent component to the farthest child component in the hierarchy. Within my main App component, there exists a Column component, ...

Guide to using the Strapi Upload Plugin: Uploading a file from a remote source using a scheduled task

Utilizing the strapi upload plugin with s3 as the provider has been a seamless experience when making API calls to the upload endpoint on my strapi instance (/upload). However, I am facing a dilemma with a cron job within our repository that monitors image ...

Vue.js routing and mixin dilemma

Calling all Vue developers! I am currently working on a vuebnb tutorial and running into an issue with routing and mixins. I have attempted to assign data before entering the router using the beforeRouteEnter guard, but it seems like my template is being r ...

Using the forEach method, we can create multiple buttons in ReactJS and also access the onClick handler

I have a button with both the label and onClick properties. Additionally, I have an array containing the values I need to assign to the label property. Here is the code snippet: render(){ {tabel_soal.forEach(function (item, index) { <Ra ...

Can you provide a detailed explanation for the Long equality condition?

Review the following scenarios: if (isCustom() == new Long(1)) { System.out.println("ok 1"); } if (1L == new Long(1)) { System.out.println("ok 2"); } In the given context: static Long isCustom() { return 1L; } The output w ...

What can be done to stop the caching of the route that router.push() redirects to in Next.js middleware?

Encountering an issue with client-side navigation and middleware in the router. It seems that the router is storing the initial redirect path and subsequent navigations bypass the middleware. This behavior ceases upon browser refresh and does not occur in ...

Bringing out the version information in a React/webpack application: A guide

Looking for a way to display the version of every build in your app? I attempted to follow a tutorial to achieve this, but unfortunately, it didn't work for me. The tutorial I used can be found here: Do you know of any other methods that could help a ...

Troubleshooting problem with image loading in AngularJS using ng-repeat

Recently delving into using AngularJS in my projects has presented a rather significant issue when utilizing ngRepeat to load thumbnails from a dynamic array into a DIV. While I won't dive deep into the entire application's details here, let me ...

Retrieving the custom attribute value from the chosen option and storing it in a JavaScript variable

I am currently working on a project that involves a country select input, with custom attributes included in the options. My goal is to retrieve the value of the "name" attribute using JavaScript and then append it to a link for further processing. Howev ...

Impressive javascript - extract file from formData and forward it

Presented here is my API handler code. // Retrieve data. const form = formidable({ multiples: true }); form.parse(request, async (err: any, fields: any, files: any) => { if (!drupal) { return response.status(500).send('Empty ...

Python Selenium - Trouble clicking button element without redirecting to desired link

I am currently conducting a test on a web user interface using Selenium in Python. In one of the test cases, there is a button that should redirect to another page when clicked. However, despite executing the code without any errors, the page does not red ...

Is it simple to host a vue.js web application within the assets folder of a sails.js project?

Currently, I am in the process of learning how to utilize Sails.js and vue.js. My goal is to develop a small application where a vue.js application is situated within the assets/ directory of Sails.js. The challenge I'm facing is figuring out how to s ...

From vanilla JavaScript to the powerful lodash library

Can you help me determine if these statements are equivalent? myApp.filter('myFilter', function() { var result, i, sport, y, contains, league; return function(sports, filterBy) { if (angular.isUndefined(sports) || !filterBy) { ...

The app constantly requests permission for geolocation services

While experimenting with the geolocation API, I encountered an issue where my page kept repeatedly asking for permission upon refresh. To work around this problem, I attempted to save my coordinate data to local storage but encountered difficulties in ma ...

The process of examining a function that includes the invocation of an additional API function in a NodeJS environment

I'm faced with a situation where I have a nested function inside another function. The second function is responsible for making an API call. However, in order to write a unit test for this scenario, I need to avoid actually making the real API call a ...

Making Jquery functions work with Internet Explorer (including toggle and animate)

Why is this jQuery code snippet not functioning as expected in Internet Explorer? It works flawlessly across all Webkit browsers. $('#logo').toggle(function() { $('#about').animate({'top': '-400px'},'slow&a ...

Could someone provide a detailed explanation of exhaustMap in the context of Angular using rxjs?

import { HttpHandler, HttpInterceptor, HttpParams, HttpRequest, } from '@angular/common/http'; import { Injectable } from '@core/services/auth.service'; import { exhaustMap, take } from 'rxjs/operators'; import { Authe ...

Having trouble getting a local npm installation to work from a specific file path?

After following the instructions from this helpful link to install an npm package through a file path, I encountered an error when attempting to use it: Cannot find module '<module_name>' or its corresponding type declaration Are there an ...

Is there a way for me to retrieve the name and extension of an attached file in an input field of type "text"?

Could you assist me with a task? I have a table and I would like to extract the name and extension of each file and insert it into an input field below each "file" type input. This information will then be saved into a MySQL database, allowing me to create ...

Selenium Visualiser with Offset Coordinates

After mastering the move_by_offset(x, y).perform() command, I find myself questioning the significance of x and y. Are these coordinates that we plot on an axis where x and y represent their respective positions? ...