Leveraging JavaScript executor for inputting text into an xpath element

Currently utilizing selenium, I am faced with the task of writing to an xpath element. Up until now, I have successfully executed a click on it using the following code snippet:

driver.findElement(
     By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Vehicle:'])[1]/following::ul[1]"))
     .click();

Following the click action, my next requirement is to write on the same element. For this purpose, I have been employing the code below:

WebElement ele = driver.findElement(By.xpath("(.//*[normalize space(text()) and normalize-space(.)='Vehicle:'])[1]/following::ul[1]"));

     ((JavascriptExecutor) driver).executeScript("arguments[0].value='SED'",ele);

Although in the above example I attempted to input 'SED', unfortunately, I am encountering issues getting any text entered into the field.

Answer №1

After correctly identifying your element and using the .click() method to access the textbox, you can utilize sendKeys() to input the value "SED". No need for a separate JavascriptExecutor instance in this scenario.

For example:

driver.findElement(By.xpath("(.//*[normalize space(text()) and normalize-space(.)='Vehicle:'])[1]/following::ul[1]")).sendKeys("SED");

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

Creating a promise in an AngularJS factory function to perform an operation

When working within an Angular factory, I am tasked with creating a function that must return a promise. Once the promise is returned, additional functionality will be added. SPApp.factory('processing', ['$http', '$rootScope&a ...

When using $dialogs.create on my website, a popup form appears with specific formatting limitations dictated by the defining code

When a user clicks a button on my website, a function in the controller is triggered. Here is the function that runs when the button is pressed: $scope.launch = function(idOfSpotOfInterest, schedOfSpotOfInterest){ var dlg = null; dlg = $dialogs. ...

Animating the rotation of a Three JS mesh to a set of precise coordinates within space

Currently, I am working on a project involving an airplane model that is initially facing a default location along the Z axis. My goal is to create a functionality where, upon clicking a button, the airplane will smoothly turn towards a specific vector at ...

Turn off Highlighting on a Website

I conducted an experiment similar to this: <div onclick="clickfunc(this)"> highlightME </div> <div> ooo blablabla highlightME ooo highlightME ooo highlightME ooo</div> <script language=javascript> function clickfunc(obj ...

javascript string assignment

Is it possible to conditionally assign a string based on the result of a certain condition, but for some reason, it's not working? var message = ""; if (true) { message += "true"; } else { message += "false" } console.log(message); ...

The state value is not being updated by the useReducer function

Hey there! I had this idea to add a black transparent overlay with text to my page while it's waiting for data from the server. I wanted the overlay to cover 100% of the page and be 30% black with the text "Loading ...". I set it up using useState, b ...

Experiencing an issue where AngularJS $watch function is executing twice, making it difficult to compare old and new values for a

I'm working with AngularJS and I'm trying to monitor a variable for changes in order to decrease another variable. This is the code snippet I have so far: $scope.$watch('watchMe', function () { $scope.decreaseMe--; ) The watchMe v ...

Formik: encountering target as undefined while passing Material UI Date Picker as prop to React Component

I'm currently working on developing a component that can be reused. The idea is to pass form fields as props, but I ran into an issue when clicking on the datepicker field. The error message that pops up is: TypeError: target is undefined Any sugge ...

Choose the right Vue.js component for optimal performance

I have a primary element within which there is a secondary element with vue.js 2.0. The issue arises when the secondary element relies on methods from the primary element. Here's an illustration: Vue.component('primary-element', { tem ...

The React Material-UI Slider does not move when OnChangeCommitted is triggered

Struggling with implementing the Material UI slider in React, I encountered an issue where the OnChange function would trigger every time the value changed. I needed it to activate only when the left mouse button was released. Fortunately, I discovered a s ...

Error: Selenium unable to locate Firefox user profile

Recently, I embarked on my journey of learning Selenium on a Linux server. My first step was to open two Putty terminals and navigate to the directory where my files are located. In terminal 1, to start the server, I executed the following command: DISPL ...

What is the best way to hold off until the content of an element changes to a value other than a string?

Currently, I am utilizing Selenium with Python 3 to extract content from an element on a webpage. The challenge is that the text within this particular element is dynamic. <div id="accResp">Please wait...</div> The text in the "Please wait... ...

Stop allowing users to place periods before their nicknames on Discord servers (Programming in discord.js with a Discord Bot)

I have been working on a script to identify when a user alters their name on Discord by adding a period at the beginning, such as changing "bob" to ".bob". The goal is to prevent this change and keep it as "bob". if (user.nickname.startsWith(".")) { ...

Unusual occurrences of backslashes in arrays when using JSON.stringify

While experimenting with JavaScript, I observed an unusual behavior when inserting a backslash \ into a string within an array and using JSON.stringify() to print it. Normally, the backslash is used for escaping special characters, but what if we actu ...

BookshelfJS: Establishing a One-to-One Relationship

Within my database, I am working with two tables - User and Address. The User table consists of the following two methods: shippingAddress: function() { var Address = require(appRoot + '/config/db').model('Address'); return thi ...

Unordered calling of functions in JavaScript - is it possible?

I'm currently working on a project that involves extracting data from an SQL database and converting the output of a query (which is a number) into a corresponding color, which is then passed to a JavaScript variable. Essentially, I am using ajax to ...

Troubleshooting a Vue.js formatting problem in Visual Studio 2019

Encountering an issue with VS2019 while attempting to format this code section. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="milestone.ascx.cs" Inherits="uc.dms.milestone" %> <section class="content-header"> <h1> ...

Is there a way to display a modal newsletter popup just one time during a user's session?

I am trying to implement a modal popup that only shows once per session, but I am facing an issue where the div pops up every time I visit the homepage. My attempt to use cookies to fix this problem has not been successful. <script> if (!Cooki ...

Why is Facebook giving me undefined instead of my profile in Node.js?

I am working on implementing authentication using Google and Facebook in my application. The front-end is built with AngularJS and the server is powered by node.js. Below is the Facebook authentication function in my node.js server: var User = require(&a ...

What could be the reason why the module is not being detected in Angular's TestBed?

Could you please explain why the module is not found in Angular? Here is my code: https://codesandbox.io/s/8k7vlvl5q2 The issue seems to be on this line: <<import { ComponentFixture, async, TestBed } from "@angular/testing"; >> TestBed canno ...