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

Generating ChartJS in Real-time

I have a straightforward form where the user selects a start date and an end date. Once these dates are selected, the tool automatically fetches data from a website in JSON format. Below is the code for my Angular controller: (function () { angular.m ...

When pressing the next or previous button on the slider, an error message pops up saying "$curr[action] is not a

I found this interesting Js fiddle that I am currently following: http://jsfiddle.net/ryt3nu1v/10/ This is my current result: My project involves creating a slider to display different ages from an array, such as 15, 25, 35, 45, 55. The goal is to show ...

Minifying Angular using grunt leads to 'Error initializing module' issue

Currently, I have multiple controllers, models, and services set up as individual files for preparation before minification. My goal is to combine and minify all these files into one JS file for production. To illustrate how my files are structured, here ...

Sending a JavaScript string to a PHP script from a Chrome extension content script

I am currently developing a chrome extension that is designed to extract text data from specific websites when I visit them, and then store this data in a SQL database. The JavaScript code for data extraction is functioning correctly and is able to capture ...

The Vue.js v-on:mouseover function is not functioning properly in displaying the menu

When I hover over a LI tag, I want to display a menu. I have successfully implemented it using a simple variable with the following code: @mouseover="hoverFormsControls=true" @mouseleave="hoverFormsControls=false" However, when I attempted to use an arr ...

You cannot directly convert a JSON Array into a JSON Object on an Android platform

I am facing an issue with populating a spinner from JSON data. I have successfully printed the response in the log cat, but I am unable to fetch it in the Spinner component. Here is the JSON format that I am working with: JSON : [ { "cat_id": ...

Following my ajax submission, the functionality of my bootstrap drop-down menu seems to have been compromised

I'm having an issue with my login page. After implementing Ajax code for the reset password feature, the dropdown menu on the login page doesn't work properly when wrong details are entered and the page reloads. I've tried using the $(' ...

Tips for executing a JavaScript function within a secure sandbox environment

My current setup involves an application server developed in JavaScript (node.js) where I receive JS function code as input from a web browser. Now, my goal is to execute this function on the server without any interference with other processes. I am look ...

assigning attributes to web addresses

Is there a way to set a style property for webpages by targeting addresses that contain /index.php/projecten/ instead of specifying each complete address in the code? Currently, I am using the following code: <ul class="subnavlist" style="display: &l ...

ElementUnreachableException: unable to interact with the element

My code is designed to autofill a form: from selenium import webdriver driver=webdriver.Chrome('driverchrome//chromedriver.exe') #Open the form driver.get('https://forms.gle/A3qCjSCXi5H64qA26') #Add name driv ...

What is the best way to insert a chart into a div using *ngIf in Angular?

I just started using chart.js and successfully created the desired chart. However, when attempting to implement two tab buttons - one for displaying tables and the other for showing the chart - using *ngIf command, I encountered an error: Chart.js:9369 F ...

Ajax: setInterval function successfully runs code but does not refresh the HTML output

I have multiple div elements in my code. I want to update the HTML content inside them based on an API request. The updating process works fine, but the HTML content doesn't refresh visually (meaning that even if I receive a new result from the API, t ...

Tips for resolving SSL certificate errors when using Selenium WebDriver with Internet Explorer

Currently, I am a beginner in utilizing Selenium Web Driver with Java. Successfully launching an application, but encountering SSL certificate errors. Seeking guidance on how to address this obstacle as I continue to learn and prepare for implementation at ...

What could be the reason for my user input not being captured and saved as variable data on the HTML webpage?

Here is the code I am working with: var g = document.getElementById("al").value; function start() { document.getElementById("test2").innerHTML = typeof(g) + parseFloat(g); } <p id="test2">Output will be displayed here:</p> <form> ...

Steps for choosing the nth HTML row with jQuery

I'm facing a situation where I need to be able to select the nth row of an HTML table based solely on the id of the selected row. You can see exactly what I mean by checking out this JSFiddle Demo <table class="mytable1"> <tr><td i ...

best practice for parsing JSON strings in Java

I have a JSON string/response that I need to parse in a simple way to extract objects/arrays. The structure may be complex and repeated, so I need to retrieve the data list by list. Most parsers only work with basic JSON structures, but mine is a bit mor ...

Error encountered when attempting to retrieve JSON data in JavaScript due to being undefined

A snippet of code that reads and writes JSON data is presented below: var info; $(function () { $.getJSON("data.json", function (d) { info = d; }); $('.btn').click(function () { info['c-type'] = $('#c- ...

What is the best way to activate a click event within an ng-repeat loop in AngularJS

Is there a way to trigger an event click handler in angular where clicking the button will also trigger the span element? I've tried using the nth-child selector without success. Any suggestions on how to achieve this? I also attempted to use jQuery s ...

Angular and JavaScript Performing Slide-Up Animation

Currently, I am working on creating a menu using Angular and JavaScript. My goal is to have this menu toggle between showing and hiding when a button is clicked. If you would like to view the code that I have written so far, you can check out the demo her ...

Manipulate the <title> tag using jQuery or PHP

I have my own personal blog and I am trying to change the title tag dynamically when viewing different blog posts. My goal is to have the post title show up in the Twitter tweet button. I attempted the following methods: <script type="text/javascript"& ...