Using JavaScript with Selenium, you can easily clear the input field and send

Here is a question input field. When clicked on, the h3 element with the name will disappear and the input tag will appear for entry of a new name.

<td style="width:92%;">
                                    <h3 id="question_text_1846" onclick="return onClickQuestion(1846,'text');">test name</h3>
                                    <input type="text" id="question_text_input_1846" onkeypress="return OnKeyPress(event, 1846,'text');" name="question_text_input_1846" onblur="return onBlurQuestion(1846,'text');" placeholder="Question Text" value="test" class="form-control myInput" style="display:none;" />
                                    <script type="text/javascript">
                                        $("#question_text_1846").html(unescape($("#question_text_1846").html()));
                                    </script>
                                </td>
    

I am trying to set a new name by clearing the field and using sendkeys through selenium and possibly some JavaScript manipulations. However, the methods I have tried so far have not been successful.

element = driver.findElement(By.xpath("//h3[@id='question_text_"+ExtractQuestionTextInputID()+"']"));
            actions = new Actions(driver);
            actions.moveToElement(element);
            actions.perform();
            element.click();

    element = driver.findElement(By.xpath("//input[@id='question_text_input_"+ExtractQuestionTextInputID()+"']"));

            element.clear();
    element.sendKeys("test_question_one");
    

I have also attempted it with JavaScript

String qtid = "question_text_" + ExtractQuestionTextInputID();
            String qtiid = "question_text_input_" + ExtractQuestionTextInputID();
            js.executeScript("document.getElementById("+qtid+").setAttribute('style', 'display: none;')");
            js.executeScript("document.getElementById("+qtiid+").setAttribute('style', '')");
    

This method extracts the id from the tag attribute

public String ExtractQuestionTextInputID(){
            String question_text_input_id = driver.findElement(By.xpath("//input[@value='New Question']")).getAttribute("id");
            Pattern p = Pattern.compile("\\d+");
            Matcher m = p.matcher(question_text_input_id);
            String mid = new String();
            while(m.find()) {
                //System.out.println(m.group());
                mid = m.group();
            }
            return mid;
        }
    

Answer №1

It seems like there might be a solution by setting the 'value' attribute of the input to an empty string. Have you given that a try?

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

Concealing the BrowserStack key within Karma

Currently in the process of developing a JavaScript application, I am running tests using Karma on BrowserStack with the assistance of the karma-browserstack-runner. According to the guidelines, the accessKey and username should be included in the karma co ...

What are the required parameters for initializing an instance of the SQLiteOpenHelper class in Android Studio?

I'm currently working on a project where I need to delete a SQLiteDatabase entry using a button. So, I have set the onClick attribute to "deletar" on the button: public void deletar (View view){ Dados_familiaOpenHelper dados_familiaOpenHelper = ...

How can I get video playback in IOS to work with Videogular2 using HLS?

I recently integrated videogular2 into my Angular 6 app to display HLS streams. Everything seems to be working smoothly on desktop and Android devices, but I encountered an error when testing on IOS: TypeError: undefined is not an object (evaluating &apos ...

How can you include a key event handler that specifically looks out for the Space key being pressed?

When a user presses the Enter key (and button is in focus), the button opens. However, I would like to also open the link button when the user presses the space bar. Buttons should be activated using the Space key, while links should be activated with the ...

Do class bindings with variables need to be inline when using Vue 3?

Consider the following HTML: <template v-for="(child, index) in group"> <div :class="{'border-pink-700 bg-gray-100 ': selected === child.id}"> <div>Container Content</div> </div> & ...

What could be causing my Ajax JSON data to not return accurately despite appearing correctly in the console logs?

My function is designed to retrieve a number (1,2,3, etc.) based on latitude and longitude coordinates: function getNumber(lat,lng) { var params="lat="+lat+"&long="+lng; $.ajax({ type: "POST", url: "https://www.page.com/cod ...

Combining Objects within an Array in JavaScript When Certain Conditions Are Satisfied

In my scenario, I am seeking assistance with merging the values of objects in an array if the id matches the warehouse_item_id. Specifically, there are two objects that need to be merged: id 191 and id 52 because id 52 has a warehouse_item_id of 191. Ple ...

Is it possible for Robot Framework to facilitate keyword-driven testing for a GUI developed in C#?

Just dipping my toes into the world of automated testing! I'm wondering how to create keyword-driven tests with the robot framework for a GUI developed with .NET. Feeling a bit unsure about this process. ...

Saving modified input data for submission using Vue

Objective: The main goal is to have a form interface loaded with an object's current data for editing. This allows the user to open a modal with the form already filled out with the existing information, giving them the option to either edit it or lea ...

Tips for retrieving data from Angular dropdown menus

How to Retrieve Selected Value in AngularJS HTML <select data-ng-model='datasource.value' ng-options='sourcetype.dataSourceType as sourcetype.dataSourceDesc for sourcetype in sourcetypes' data-ng-change="getAccountCredentials(sourc ...

The mapStateToProps function is returning an undefined value

import React, { Component, Fragment } from "react"; import { connect } from "react-redux"; import { login, logout } from "./redux/actions/accounts"; import Home from "./Home"; import Login from "./Login"; class ToggleButton extends Component { render() ...

Encountering an issue with this error message: 'Building Workspace' has hit a snag and encountered errors during the build process

When working with Eclipse Neon and Maven 3.5.0, I encounter an error every time I try to save changes made to the pom file. This issue is preventing me from moving forward in my project as I am unable to resolve it. ...

Is there a way to restrict the orientation of a UIWebView using JavaScript or HTML?

Currently, I am working on HTML content for an iPad application. The challenge at hand is locking the UIWebView's content to portrait mode. Despite having already submitted the app to Apple, I have only come across a solution in this question, which s ...

Display or conceal a <div> segment based on the drop down selection made

A dropdown menu controls the visibility of certain div elements based on the selection made. While this functionality is working for one dropdown, it's not working for another even though the code is very similar. I've tried various solutions but ...

Step-by-step guide on setting up a fresh Chrome profile using Selenium in C#

Please refrain from labeling this question as "Duplicate" as it is unique. I am inquiring about the process of creating Chrome profiles, not just opening them. Your assistance would be greatly appreciated. I am seeking guidance on how to programmatically ...

"Invalid operation" error encountered within a Flask function in Python

I am trying to store a 'person' resource in a .ttl file using a JavaScript function: Here is my SPARQL query: @app.route('/registraAnnotatore/<author>+<mail>', methods=['POST']) def registraAnnotatore(author, ...

Implementing specific CSS styles for images that exceed a certain size limit

Currently, I am facing a dilemma as I work on developing a basic iPhone website that serves as a port for my blog. The main issue I am encountering is the need to add a border around images above a specific size and center them in order for the blog to hav ...

Combine a JSON object and a JSON array by matching the value of the JSON object to the key of the JSON array

I am looking to create a JSON array in node by combining two JSON objects and arrays. `var template = { "key1": "value1", "key3": "value3", "key4": "value3", "key6": "Dummy Value1" }; var data = [ { "value1": "1", "value2": "2", ...

The input type "number" does not seem to be compatible with the currency pipe feature

The currency pipe seems to not be working when using the input type number. The web page is not displaying any value. I have searched various blogs and it appears that the currency pipe only works with input type text, but my requirement necessitates the ...

Troubleshooting minified JavaScript in live environment

Trying to wrap my head around AngularJS and Grunt. In my GruntFile.js, I have set up two grunt tasks for development and production. In production, I am using uglification to combine multiple js files into one. I am in need of some advice or tips on how t ...