Having trouble with the "setValue" property being undefined error. Looking for a solution to input text into a codeMirror element using Selenium WebDriver with Java

Currently, I am attempting to input text into a textarea field that is recognized as CodeMirror. Below is the code snippet I have been working with:

{...

WebElement scriptField = 

this.getDriver().findElement(By.cssSelector(".CodeMirror-line>span"));

JavascriptExecutor js = (JavascriptExecutor) this.getDriver();

String query = "text";

js.executeScript("arguments[0].CodeMirror.setValue(\""+ query +"\");", scriptField);

}

However, I encountered an error on the last line of my code:

org.openqa.selenium.WebDriverException  : unknown error: Cannot read property 'setValue' of undefined

I am unsure which webelement the scriptField variable should be referring to. When I manually type text, it appears in this element: ".CodeMirror-line>span". Can someone confirm if my understanding is correct?

An excerpt of the DOM structure can be seen in the attached image below.

https://i.sstatic.net/e8I1a.jpg

Answer №1

After obtaining the WebElement, named scriptField for this example, you can easily input text using the sendKeys() method.

WebElement scriptField = this.getDriver().findElement(By.cssSelector(".CodeMirror-line>span"));
scriptField.sendKeys("input your text here");

If needed, you have the option to clear the field before entering your desired text.

WebElement scriptField = this.getDriver().findElement(By.cssSelector(".CodeMirror-line>span"));
scriptField.clear();
scriptField.sendKeys("input your text here");

Answer №2

To update the content of the span element with text, you can use the following method:

driver.executeScript("arguments[0].innerText = " + message + ";", elements);

Answer №3

If you want to update the current text with newText, you can accomplish it by using this code snippet:

WebElement textElement = this.getDriver().findElement(By.cssSelector("pre.CodeMirror-line>span"));
JavascriptExecutor executor = (JavascriptExecutor) this.getDriver();
String script = "arguments[0].innerHTML='newText';";
executor.executeScript(script, textElement);

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

AHK Chrome Extension for Selecting Items from Dropdown Menus

Is there a way to use AHK codes from https://github.com/G33kDude/Chrome.ahk to select an option from a dropdown list? I've tried the following code, but it doesn't seem to work: document.getElementsByName(""cars"")[0].options ...

Enhance the size of dynamically generated svg elements by scrolling the mouse wheel in and out

In my ASP.NET application, I have a view page where SVG elements are dynamically generated. Now, I want to implement zoom functionality for all the created SVG elements. Zooming in should occur when scrolling up and zooming out when scrolling down. <sv ...

Instructions for receiving user input and displaying it on the screen, as well as allowing others with access to the URL to view the shared input provided by the original user

<h1>Lalan</h1> <input type="text" name="text" id="text" maxlength="12"> <label for="text"> Please enter your name here</label> <br><input type="submit" value ...

What is the best way to access a variable from a .js file?

Is there a way to access a variable defined in a JavaScript file from a Vue file and pass it to the Vue file? In the following code snippet, there is a template.js file and a contact.vue file. The template file converts MJML to HTML and saves the output to ...

Executing multiple instances of selenium is resulting in connection issues

In my selenium automation setup, I am using python3 on Linux with undetected_chrome and proxies. Everything works fine when running a single instance, but as soon as I launch a second one, I encounter the following error: 2024-04-30 00:09:28 - ERROR - HTTP ...

Generate a variety of requests with Axios

I have a task where I need to send multiple Axios requests, but the number of requests can be completely random. It could range from 0 to even millions. Once all the requests are completed, I then need to perform an action, such as updating my state, which ...

Posting several pictures with Protractor

In my test suite, I have a specific scenario that requires the following steps: Click on a button. Upload an image from a specified directory. Wait for 15 seconds Repeat Steps 1-3 for all images in the specified directory. I need to figure out how to up ...

Discovering the optimum route within a 2D array given specific limitations using Dynamic Programming

Hey, I have a query related to dynamic programming (dp) that goes like this: Input: A 2D array of numbers Output: The maximum sum of a path from (0,0) to (n-1,n-1) with the following conditions: You can only move down and right i.e. from (A[i-1][j]) t ...

What is the method for setting the content-type in an AJAX request for Android browsers?

I am facing an issue with my ajax call to the Rails server. The response from the server varies between HTML and JSON based on the content type. Surprisingly, this works smoothly on iPhone and desktop browsers like Chrome, but I am encountering a problem o ...

An attempt was made to access the method com.google.common.util.concurrent.SimpleTimeLimiter erroneously while using Maven, resulting in a java.lang.Illegal

I encountered an error when running my scripts. The specific error message is: java.lang.IllegalAccessError: tried to access method com.google.common.util.concurrent.SimpleTimeLimiter.<init>(Ljava/util/concurrent/ExecutorService;)V from class org.ope ...

Learn how to utilize Vue 3 to access properties that have been passed down from a parent component, even if they

Hey there, hope everything is going well. I'm familiar with react.js, but when I gave vue a try, things felt a bit different. In react, it's easy to access props passed from the parent in the child component without much hassle. However, in vue, ...

Creating interactive button groups with responsive design in a Material UI and ReactJS web application

Is it possible to make ButtonGroup Buttons responsive? I heard about an attribute called "Orientation" in material-ui's ButtonGroup, but I'm not sure how to use it with media queries for changing orientation based on the device width. I'm st ...

Node.js and Express causing Openshift to enter a crash loop back-off situation

I've recently ventured into setting up a server for dynamic web pages using Openshift. I put together a basic node.js/Express template solely to establish a connection to Index.html for testing purposes. However, whenever I attempt to execute the code ...

"Is it possible to differentiate between a variable that is BehaviorSubject and one that is not

I am dealing with a variable that can be of type Date or BehaviorSubject<Date | null>. My concern is figuring out how to determine whether the variable is a BehaviorSubject or not. Can you help me with this? ...

There seems to be an issue with posting JSON data correctly

Currently, I am attempting to include an object with numerous attributes within it. This is the object I am trying to use with $.post: ({"fname" : fname, "lname" : lname, "gender" : gender, "traits" : { "iq" : inte ...

Tips for hiding a div element until its visibility is toggled:- Set the display property of

Looking for some guidance on this jQuery code I'm working with to create a toggle menu. The goal is to have the menu hidden when the page loads, and then revealed when a button is clicked. However, currently the menu starts off being visible instead o ...

Maximizing the efficiency of rendering components in Ember JS

I have a situation where I need to render thousands of components inside a wrapper component, but currently only around 300 components are being rendered due to performance issues. How can I achieve this without any rendering delays or performance proble ...

Error: The method api.createContextKey is not defined while running the Next.js development server

I am currently facing an issue while attempting to start the development server for my Next.js project. The problem arises when I try to incorporate MUI into my project, resulting in these specific errors. The error message points to a TypeError related to ...

Webpack Is Having Trouble Parsing My JavaScript Code

I recently started using webpack and I'm struggling to get it to work properly. I haven't been able to find anyone else experiencing the same issue as me. Every time I attempt to run "npm run build" to execute webpack, I encounter this error: ER ...

"Implementing Vue mousemove functionality only when the mouse button is pressed

Is it possible to initiate a mouse movement only after the element has been clicked first? I am exploring this functionality for an audio player timeline. .player__time--bar(@mousedown="setNewCurrentPosition($event)") .slider(role="slider" aria-valuem ...