Completing an online form using Selenium on a separate webpage

For my homework assignment, I am working on a basic web automation project using JavaScript and selenium. The goal is to click on a link and fill out a form on the following page. However, I am facing an issue where even though I can locate the web element correctly, I am unable to fill in the form.

I have attempted to use both Xpath and CSS selectors to identify the web element.


const driver = new selenium.Builder().forBrowser("chrome").build();

const form = driver.findElement(By.linkText("Form Authentication")).click;

const username = driver.findElement(By.xpath("//form/div/div/input[@id = "username"]")).sendKeys("some text");

driver.get(url);

The expected outcome should be that the Username/password fields are filled with some text. However, despite no errors being displayed, no action is taken.

Answer №1

Here is a code snippet that needs to be corrected:

const username = driver.findElement(By.xpath("//form/div/div/input[@id = "username"]")).sendKeys("some text");

To fix the issue, please update the code as shown below:

const username = driver.findElement(By.xpath("//form/div/div/input[@id = 'username']")).sendKeys("some text");

The error was caused by using double quotes within double quotes. The correction should resolve this issue.

I hope this solution resolves your problem :)

Answer №2

Upon reviewing the code, several issues have come to light. The variable url appears to hold the URL for the login page at runtime. To ensure proper functioning, it is recommended to navigate to the URL first before filling in any fields.

const driver = new selenium.Builder().forBrowser("chrome").build();

driver.get(url);

Additionally, attention should be paid to the correct usage of JavaScript methods.

const form = driver.findElement(By.linkText("Form Authentication")).click;

The above code snippet assigns a reference to the click function to the form variable, which may not be the intended action. Consider directly clicking the link:

driver.findElement(By.linkText("Form Authentication")).click();

Furthermore, there seems to be an issue with double quote nesting:

const username = driver.findElement(By.xpath("//form/div/div/input[@id = 'username']")).sendKeys("some text");

To address this, utilize single quotes around @id = 'username'.

Moreover, since the id attribute in HTML is inherently unique, simplification of the last XPATH expression is possible:

By.xpath("//input[@id = 'username']")

A crucial step that appears to be missing involves submitting the form after entering the username and password. This can typically be accomplished by clicking on a submit button.

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

What is the best way to delete the <li> element from a webpage

Here is the HTML code snippet that I am working with: <form name="package_type_documents" action="" method="post" enctype="multipart/form-data"> <div class="hor-form"> <ul> <li> <div class="answ ...

JavaScript Lint Warning: Avoid declaring functions inside a loop - unfortunately, there is no way to bypass this issue

In my React JS code snippet, I am attempting to search for a value within an object called 'categories' and then add the corresponding key-value pair into a new map named sortedCategories. var categoriesToSort = []; //categoriesToSort contains ...

React - Clearing State data retrieved from axios request

I am currently facing an issue with resetting the state of an object in my users array upon clicking the delete button. Even after successfully removing the object from the database, the state does not update as intended. I have managed to verify the prese ...

Struggling to comprehend the node.js walker concept

I am struggling to grasp the concept of how the signature/header of the node.js walker functions. I understand that a walker can go through a directory and apply filters, but I'm unsure about how the signature of the .on function works. For example: ...

"Utilizing Bootstrap Tour for Easy Navigation: Automatically Scroll Back to the Top with

I have a Bootstrap tour set up with code that is meant to capture the user pressing the end tour button and scroll them back to the top of the screen. However, currently the code runs when a new popover loads instead of when the end tour button is clicke ...

How to seamlessly adjust the height from 0 to auto

My goal is to smoothly transition from height: 0; to height: auto; and reveal the content inside a <div></div>. One effective method I've come across involves using the max-height property. Here's an example of how it can be implement ...

The ES6 method of binding click handlers with parameters in React

After reading numerous articles on the usage of () => {} syntax, binding in the constructor, and binding in the props, I have come to understand that binding this can be performance-intensive. Furthermore, automatic binding with arrow functions incurs a ...

I am encountering an issue where JSON.parse is returning undefined when trying to

Upon receiving a string from my server, I am attempting to parse it into a JSON object. Here is the string and relevant code snippet: stringToParse = "\"{'female': 16, 'brand': 75, 'male': 8}\"" d ...

Create a Django model that utilizes recursive parsing of .text output from selenium

After successfully retrieving the necessary data using Selenium in Python, my next challenge is figuring out how to recursively process the text that follows a consistent structure. The output from Selenium has been saved to a .txt file for further analysi ...

The length of the HTTP response in Angular is not defined

Currently, I am utilizing Angular in conjunction with jQuery Mobile to develop multiple pages, but I have encountered an obstacle when using the $http service to connect to my JSON requests. While populating a few arrays with functions and successfully ret ...

Mongoose: When encountering a duplicate key error (E11000), consider altering the type of return message for better error handling

When trying to insert a duplicate key in the collection, an error message similar to E11000 duplicate key error collection ... is returned. If one of the attributes is set as unique: true, it is possible to customize this error message like so: {error: ...

Struggling to make changes to a Styled Component in a React application

In a certain file, I have a BaseComponent composed of various other components and being exported. The top level of the BaseComponent contains a wrapper responsible for managing margins. When I export it and attempt to override some styles with: //other fi ...

I am unable to display the content even after setting `display: block` using the `.show()`

Hello there, I have attached my javascript and html code. While in debug mode, I can see that the CSS property 'display: none' changes to 'display: block', but for some reason, the popupEventForm does not open up. Any suggestions on why ...

Encountered a POST Error while attempting to access a backend function from the frontend in a React application, resulting in

I have developed a website using React for the frontend and Node.js Express for the backend. However, I am facing an issue where calling a backend function from the frontend takes around 2 minutes and results in the following error being displayed in the c ...

Triggering a state in Reactjs from a different component: A step-by-step guide

Hey there, I could really use some assistance with this situation. Currently, I have a progress bar component and another component where I utilize the progress bar. Additionally, there is a third component that is meant to initiate the progress bar. Let ...

Displaying numerous Google charts within a Bootstrap carousel

A bootstrap carousel has been implemented to showcase our company's data. The carousel includes a bootstrap table, images, and two Google charts: a pie chart and a stacked bar chart. The issue arises when the active class is not maintained for the Go ...

Tips on updating a JSON file exclusively using vanilla JavaScript with an AJAX call using the HTTP POST method?

Question One: I have been struggling to find a way to update a JSON file using the HTTP POST method when both files are stored on the same server (e.g. Godaddy or AWS). While I am able to send data to the server successfully, the JSON file is not being upd ...

Is it possible to execute a Greasemonkey script multiple times on a single page?

As someone who is new to Greasemonkey, JavaScript, and all things UI-related, I am seeking assistance. I have a requirement where the Userscript needs to run once by Greasemonkey after the page loads. However, I also need the same script to be executed mu ...

Create visual representations using the data displayed in charts generated by Chart JS

Good evening. I am currently utilizing Chart JS in my project to visualize the total count per column in a bar graph. My backend framework is Laravel, and I pass the data from the controller using the variable $datacount. This snippet shows the query in ...

Voice crashes in Discord.js after the audio has been playing for a short while

Every time I try to play a song, the bot crashes and gives an "aborted" error message after about a minute. music = { "resource": createAudioResource(ytdl(parameters[0], {filter: "audioonly"}), {inputType: StreamType.Arbit ...