How can I send input to a specific input field within a div using Selenium?

Currently, I am facing an issue where I am attempting to enter text into an input field. However, I have noticed that there are two elements with the same id name and I need guidance on specifying which specific div -> input I want to interact with.

Here is what I have tried so far:

it('Entering First Name', function (done) {

    browser.driver
        .then(() => browser.wait(EC.visibilityOf(element(by.xpath('//input[@id="pp-cc-first-name-field"]'))), 50000, "Timed out finding 'First Name' element"))
        .then(() => element(by.xpath('//input[@id="pp-cc-first-name-field"]')).sendKeys("hello world")
        .then(() => done());
});

This is the HTML structure being used:

https://i.sstatic.net/7KYjH.png

At this point, I am unsure about the mistake I might be making as the input operation fails due to a timeout. My goal is to successfully input text into this particular element.

UPDATE!

After further investigation, it appears that I needed to switch to an iframe because there was an iframe loaded in the background preventing me from entering text into the field. I had to use

browser.switchTo().frame(element(by.xpath("//iframe[@id='cc-integrated-payment-page-frame']")).getWebElement()))

in order to successfully input text into the fields.

Answer №1

Your elements have mismatching IDs- the <div> element has a value of pp-cc-first-name-field, while the <input> element also has a value of pp-cc-first-name-field. To resolve this issue, update your code as shown below:

it('Entering First Name', function (done) {

    browser.driver
        .then(() => browser.wait(EC.visibilityOf(element(by.id('pp-cc-first-name'))), 50000, "Timed out finding 'First Name' element"))
        .then(() => element(by.id('pp-cc-first-name')).sendKeys("hello world")
        .then(() => done());
});

Answer №2

If you want to enter a sequence of characters into the input field, there are two options for using Locator Strategies:

  • Using css_selector:

    input#pp-cc-first-name[name='First name'][placeholder='First name']
    
  • Using xpath:

    //input[@id='pp-cc-first-name' and @name='First name'][@placeholder='First name']
    

Your revised code block will look like this:

it('Entering First Name', function (done) {

    browser.driver
    .then(() => browser.wait(EC.visibilityOf(element(by.xpath('//input[@id="pp-cc-first-name" and @name="First name"][@placeholder="First name"]'))), 10, "Timed out finding 'First Name' element"))
    .then(() => element(by.xpath('//input[@id="pp-cc-first-name" and @name="First name"][@placeholder="First name"]')).sendKeys("hello world")
    .then(() => done());
});

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

React child component does not refresh upon modification of an array property

In my main application, I have two subcomponents that both receive the same model property. The first subcomponent deals with a number prop from model.myNumberProperty, and the second subcomponent handles an array prop from model.myArrayProperty. When ch ...

The input field does not contain the chosen date

Utilizing the react-dates library from Airbnb for date selection has been a bit challenging in conjunction with redux form. Upon clicking the select date button, the calendar interface appears. However, upon choosing a date, the input box remains empty ins ...

Safari browser is experiencing issues with the custom file upload script

My custom upload script allows users to easily select images for upload by clicking or dragging them into the designated box. A preview of the chosen image should appear, and this functionality works smoothly in Firefox and Chrome. However, I've encou ...

Deleting files in a mongodb collection using node.js

As a beginner in mongoDB and Node.js, bear with me if the following code is not flawless. The task at hand is to delete a document from a collection using its _id. The deletion works fine in the mongo shell, but there seems to be an issue when executing t ...

The property was computed but cannot be set - specifically for a toggle component

I am currently working on developing a switch toggle component in Vue that includes a v-model and @updated. However, I am encountering difficulties when trying to update the model once the user toggles the switch. Initially, I faced an issue regarding muta ...

Node and Express continuing to send requests in a loop even after the response has been completed

After trying numerous solutions without success, my mind has reached its limit. So here I am seeking guidance. How can this seemingly simple code snippet (let's name it app.js): var express = require('express'); var app = express(); app.us ...

Verifying Angular (2+?) Compatibility: Opening and Closing Material mat-menu on Hover [GUIDE]

After extensive research, I tried various methods to hover a material menu to display its options. However, the solutions I came across were either too complicated or ineffective. Therefore, I decided to develop my own solution by combining elements of e ...

Transmitting multi-row form information through jQuery (ajax, json) in conjunction with Spring MVC

JSP: <script> var formData = []; $("#testForm").find('input').each(function() { var obj = {}; obj['name'] = $(this).attr("name"); obj['value'] = $(this).val(); formD ...

At all times, AJAX/PHP/JS will have a readyState of 1 and a Status of 0, with no text response

Recently, I've been attempting to utilize a sample of AJAX to compare form data with an SQL database from a domain at http://www.example.com. However, I'm encountering persistent issues where the readyState remains at 1 and the Status is always 0 ...

Swap out the setInterval function with a different algorithm embedded within the designated function

I have created a script that transitions images with a transparency effect in the same location. However, I am not a fan of using setInterval to call it. Is there a way to achieve the same result but perhaps with a loop inside a function that introduces a ...

The setting `ensureCleanSession: true` doesn't appear to be effective when included in the capabilities for Internet Explorer 11

Currently, I am testing a login/logout application with the help of protractor. One challenge I am facing is dealing with a popup that appears after each login/logout scenario. In order to ensure the popup appears after each login, I need to reset the IE ...

Having difficulty managing Chrome alerts using C# NUnit WebDriver

Lately, we've begun creating tests to run in Chrome, but we're encountering issues with managing browser-generated alerts. In Internet Explorer, we could easily handle them using the following code: var options = new InternetExplorerOptions(); o ...

Automatically update button appearance upon reaching zero value using JavaScript

Whenever I click the button, the user's HP decreases until it reaches 0 and then the button changes. However, a peculiar issue arises when the userHealth hits zero - the button does not change immediately. An additional click is required for the butto ...

Connecting Angular and Socket.IO for Server-to-Server Communication

Can a simple socket.io connection be established between a client server running on Gulp and another server running on Node? .--------. .----------. .----------. |SERVER A| | | | SERVER B | | (Gulp) | | CLIEN ...

Transmitting form information to a nested page within a div container

This is my fourth attempt to seek an answer to this question, as I've faced downvotes in previous attempts. So here we go again. I am trying to send data from a hidden input within a form using ajax. The value of the hidden input is generated by a php ...

Dynamic Text Challenge

Below is the code snippet I am working with: body{ width:100%; font-family:sans-serif; background: transparent; } .testimonials{ margin:0; display:grid; grid-template-columns: repeat(auto-fit,minmax(350px, 1fr)); grid-gap:2 ...

Error in Selenium: httplib.BadStatusLine raised

While using PhantomJS with Selenium for multiple searches on stackoverflow, I encountered an issue. The code worked perfectly on my local PC, but when I transferred it to a server with lower RAM, the httplib.BadStatusLine error occurred. This seems like e ...

What is the minimum number of lines that can be used for javascript code?

Currently, I am in the process of developing a custom JavaScript minifier. One question that has come up is whether it is necessary to insert line breaks after a certain number of characters on a single line, or if it even makes a difference at all? For i ...

Uncover and control nested objects in JSON data

Having a dynamic foo object with the possibility of nested parent objects raises the question of how to effectively: 1) Determine the last object that has a parent? 2) Create an array containing all nested parent objects along with the initial obj (foo.o ...

The casperjs evaluate function isn't able to provide me with the necessary data I

Having trouble getting my function to return data correctly. I am trying to retrieve the value of this input box. <input type="text" value="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b6e736a667b6764646025686466">[em ...