I encountered an issue with OpenQA.Selenium.WebDriverException that was caused by a JavaScript error stating that document.getElementByName is not a valid function

I encountered an error when trying to run the code snippet below:

driver = new ChromeDriver(@"C:\Users\hp\Documents\Driver");
driver.Navigate().GoToUrl("http://demo.guru99.com/v3/");
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("document.getElementByName('uid').value ='mngr303517'");
js.ExecuteScript("document.getElementByName('password').value='ujudysY'");
js.ExecuteScript("document.getElementByName('btnLogin').click()");

The error occurred at line 3.

document.getElementByName is not a function (Session info: chrome=87.0.4280.141)

What could be causing this issue?

Answer №1

When using JavaScript, remember to call document.getElementsByName(...) (plural - Elements not Element).

It's important to note that this method returns a collection of elements, rather than just one.

js.ExecuteScript("document.getElementsByName('uid')[0].value ='mngr303517'");

Make sure to access the [0] index of the collection before accessing the value property.

For cleaner code, consider utilizing document.querySelector instead:

js.ExecuteScript(@"document.querySelector(""[name='uid']"").value ='mngr303517'");
js.ExecuteScript(@"document.querySelector(""[name='password']"").value='ujudysY'");
js.ExecuteScript(@"document.querySelector(""[name='btnLogin']"").click()");

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 should I do when faced with an unexpected alert while using the Selenium Driver?

Currently, I am working on testing the functionality of www.uhc.com using Selenium with Java and Chrome browser. The website includes a random pop-up that appears while the script is executing, prompting the user to take a survey with just yes or no opti ...

Implementing Javascript to allow users to interact with a dynamic table

All user entries are captured and stored in an array. The array output should be displayed in a dynamic table which includes edit and delete options. If the number of entries exceeds 3, then the table should also have previous and next navigation options. ...

Step-by-step guide on inserting an image directly into an HTML file without utilizing LinkedResource or CDO

Let's say we have a scenario: My objective is to put together an HTML file with an embedded image, similar to the structure below: <html> <head> </head> <body> <img src="?" /> </body> </html> The quest ...

Tips for preventing the repetition of code blocks in node.js

Within my code block, I encountered a situation where the if and else blocks contain almost identical code. The only difference is due to one function call with a callback that requires repeated code in both blocks. While I am aware that creating a funct ...

Retrieving information from a JSON file results in the value of "undefined"

Currently, I am working on a data visualization project in React using Recharts. The data is sourced from a static JSON file. My aim is to enable users to click on the Bar Chart and have the selected value passed to the app. Subsequently, this value should ...

Strategies for creating a dynamic progress bar using jQuery and JavaScript

I'm currently working on a project that involves increasing a percentage number while filling up the background color inside it based on the percentage value. The current setup is functional in terms of animating the background, but I need it to dynam ...

Challenges arise when applying Animate.css in conjunction with AngularJS within OnsenUi

Incorporating Animate.css into my OnsenUI project has been quite interesting. If you want to take a look, here is the Plunkr link: http://plnkr.co/edit/b1qHW7?p=preview The task at hand: - Pressing the Present Page 2 button should reveal page2.html. ...

Is it possible to generate a PagedListPager without the need to invoke a function?

Managing a list with ajax calls has been fairly smooth on the initial load. The search button triggers an ajax call that loads the first page of results without issues. However, an obstacle arises when trying to implement PagedListPager which ends up reset ...

"Apple TV now features an iOS app that allows users to watch YouTube videos

I am interested in developing an iOS application that can play YouTube videos in a UIWebView window. Users should have the option to play the videos directly on their iOS device or stream them via AirPlay to an AppleTV. The content displayed in the UIWebV ...

Regain Identification or Data using PHP, JavaScript, and MySQL

I'm currently working on a project and I'm facing a challenge in retrieving Id either in php or js. I have 2 rows in the same table in mySql: row 1 = id_answer and row 2 = text. I am using a foreach loop in my code to extract the text from the ...

Retrieving the content of a dynamic template with Angular-UI-Router

Currently, I am in the process of developing an angular application utilizing angular-ui-router. The backend of the application consists of a REST api that provides a form URL based on a specific ticket id. My aim is to dynamically set the template in app. ...

Is there a way to set up my node package to automatically load sources without having to specifically mention the src directory?

My Node package is structured as follows: ./my-package ./src ./index.js ./a.js ./b.js README.md package.json The package.json file specifies "main": "./src/index.js" and the module loads correctly. However, to import a specific JavaScri ...

Angular Material Toolbar Experiencing Color Distortion

To see the issue in action, check out this CodePen: An ongoing problem I've encountered with Angular Material involves distorted colors on the toolbar. The edges of the toolbar display one shade of green, while the middle shows a different shade. Tak ...

Using onClick function to pass the value of an input textbox in PHP

I have an input text field and I am looking to pass the values entered inside the element through a JavaScript onClick event. <form> <fieldset> <label>Common Site ID: </label><span><?php echo $commonsiteid ?>< ...

Encountering a jQuery error while trying to utilize the $window.load

I have a code snippet that is functioning well when wrapped within a document ready event: jQuery(document).ready(function($) { $('tr[data-name="background_colour"] input.wp-color-picker').each(function() { //this section works fin ...

Accessing the extensive Excel document is proving to be quite sluggish

I'm working with a massive Excel file containing approximately one million rows and is around 400MB in size. I need to establish an IDbConnection to this file, but have encountered some challenges. So far, I've experimented with two different app ...

A specialized subclass inheriting from another subclass of unittest.TestCase, featuring a unique setUpClass method

I have a custom subclass of unittest.TestCase specifically designed for running selenium tests: class FunctionalTest(unittest.TestCase): @classmethod def setUpClass(self): self.browser = webdriver.Firefox() self.browser.implicitly ...

Is it possible to create a Three.js animation with a see-through background?

I have an HTML5 video with a greenscreen effect that I want to display on a plane using a video texture in Three.js. I attempted to use separate video textures for the main video and the alpha channel, but encountered synchronization issues. From what I u ...

NEXT JS - Application renders twice due to rewrites in place

I'm currently working on a NEXT JS project and facing the issue of the app rendering twice! Challenge After implementing rewrites in next.config.js, the app is being rendered twice. Removing the rewrites solves the issue and only renders once. next ...

Selenium is detecting a textbox as hidden, despite it being visible in the browser on my end

My password textbox code is as follows: <input class="blahblah" id="someId" type="password"></input> While I can manually interact with this textbox in the browser, when attempting to automate testing using selenium, an error is thrown sayin ...