How to ensure that the iframe is completely loaded before proceeding with Selenium JavaScript

I have a webpage that displays an iframe, within the iframe there is a spinning spinner (overlying the fields).

My approach involves using selenium to navigate to the iframe,

  return this.driver.wait(function() {
    return self.webdriver.until.ableToSwitchToFrame(self.driver.findElement(self.page.loginIframe.selector))
  }, this.config.driver.defaultTimeout);

following which I call this.driver.switchTo().frame(0); once the above condition is satisfied

however, I need to ensure that the spinner disappears and the input fields are ready for data entry.

If I utilize a driver.sleep, I can successfully input data, but if I attempt to use .elementIsVisible, it promptly tries to input data and fails.

snippet for logging in:

var self = this;
this.waitForPageLoad().then(function pageLoaded() {
    console.log("page loaded");
    self.switchToIframe().then(function switchedToIframe() {
      console.log("switched to iframe");
      // THIS FIXES the issue, but I do not want a random sleep self.driver.sleep(10000);
      self.waitForIframeLoad().then(function iframeLoaded() {
        console.log("iframe loaded!");            
        self.setUserName(self.config.user.userName);//FAILS HERE
      });
    });
});

snippet that triggers an error:

Login.prototype.waitForIframeLoad = function() {
  var self = this;
  //console.log(self.webdriver.until);
  return this.driver.wait(function() {
    return self.webdriver.until.elementIsVisible(self.driver.findElement(self.page.usernameInputField.selector))
  }, this.config.driver.defaultTimeout);
};

error message:

NoSuchElementError: no such element

Answer №1

When troubleshooting an issue, I realized that the dom of the iframe was not loading immediately, leading to the error.

Login.prototype.waitForIframeLoad = function() {
  var self = this;
  console.log(self.webdriver.until);
  var deferred = this.webdriver.promise.defer();
  this.driver.wait(function(){
    //waiting for the element to be present
    return self.driver.isElementPresent(self.page.usernameInputField.selector);
  }).then(function(){
    //checking if it is visible
    return self.driver.wait(self.webdriver.until.elementIsVisible(self.driver.findElement(self.page.usernameInputField.selector)));
  }).then(deferred.fulfill);
  return deferred.promise;
};
edit: I forgot to include the second wait function

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

The output from the Moment.js HTTP server is currently experiencing errors and is not displaying the expected

My first time working with JavaScript and the Momentjs library has not been smooth sailing. I am facing an issue where the output is not displaying as required. The goal is to show dates in the format "Day, date month year" (e.g., Tuesday, 14th May 2018). ...

Leverage the Power of AngularJS to Harness Local

I am currently developing an application using AngularJS. However, I have encountered an issue when trying to use localstorage. Here is my code snippet: var id = response.data[0].id; var email = response.data[0].email; localStorage.setItem('userId&ap ...

Engaging with JSON data inputs

Need help! I'm attempting to fetch JSON data using AJAX and load it into a select control. However, the process seems to get stuck at "Downloading the recipes....". Any insights on what might be causing this issue? (Tried a few fixes but nothing has w ...

Ways to interact with a button that toggles between states

I am working with a button that has both an enabled and disabled state. Enabled State: <button type="button" class="btt-download-csv site-toolbar-menu-button icon-download no-icon-margins ng-isolate-scope" title="Download CSV" rc-download-csv="" curren ...

The express-unless plugin in Node.js allows you to exclude specific paths from the middleware.auth

I need help implementing express-unless to exclude a /health path from using middleware.auth. Unfortunately, I am facing syntax issues and unable to test this locally. Using localSite == true is not compatible with my environment. The error logs are shown ...

Exchange of Fusion Chart

Attempting to perform a fusion chart swap with asp has proven to be challenging. The alternative approach involves rendering the fusion chart first and then using onmousedown to replace the chart with an image. However, this method is also encountering i ...

Terminate the driver using driver.quit() when an exception is not properly handled

I have a Python script that is designed to continuously run another Python script in a loop. The way I execute it is by using the command python loop.py script.py The secondary script, script.py, utilizes selenium and chromedriver for its operations. My g ...

The C# MVC Controller is having difficulty retrieving decimal or double values from an Ajax POST request

Having trouble sending decimal or double values via ajax to my C# MVC Controller. The values always come through as null, even though they work fine when sent as strings or integers. Why is this happening? When checking the client's request, the corre ...

Handling multiple render calls and rerenders in React function components with setTimeout (best practice for firing multiple times)

Is there a way to optimize the Notification component in my App, so that the setTimeout function is only initialized once even if multiple notifications are pushed into the state? function Notification(props) { console.log("Notification function compone ...

The .drop() function in pandas seems to be ineffective in removing a specific row from the

As a newcomer to coding (just in my first month), I've been working on developing a simple script for logging into Instagram. The idea is for the script to retrieve login credentials from a CSV file and automatically log in to Instagram. Below is the ...

How can parameters be included in ng-href when using ng-click?

So I have this list of products and I want to pass the current product id when clicking on ng-href by using a function with ng-click in the ng-href. This is what my html file looks like: <div class="agile_top_brands_grids" ng-model="products" ng-repe ...

Dynamic jQuery slideshow featuring a variety of animations applied to individual elements

I'm interested in creating a custom jQuery slideshow that involves animating HTML elements within each slide differently. Specifically, I would like to have 3 divs sliding out to the left with delays on 2 of them, and then being replaced by equivalen ...

Guide on implementing iterative data path in v-for loop using Vue

I'm just starting out with Vue and I want to use image file names like "room1.jpg", "room2.jpg", "room3.jpg" in a loop Below is my code, where the second line seems to be causing an issue <div v-for="(p,i) in products" :key="i"> <img src ...

Selenium is having trouble reading the text from a dropdown that has the autocomplete feature disabled

It seems that Selenium is having trouble retrieving the text from the dropdown options. Here is a snippet of the code I am working on: WebElement dropdown=driver.findElement(By.id("selFromAccount")); List<WebElement> dropoptions=dropdown.findElemen ...

Tips for creating dynamic alerts using mui v5 Snackbar

My goal is to call an API and perform several actions. After each action, I want to display the response in a Snackbar or alert. Despite iterating through the messages in a map, I'm only able to show the first response and none of the others. Here is ...

reqParam = $.getQueryParameters(); How does this request work within an ajax form

I've spent a lot of time searching, but I can't figure out what the code reqParam = $.getQueryParameters(); means and how it is used in Ajax JavaScript. I copied this Java script from a website as an example. If anyone has any insights, please he ...

Invoke the function defined within a modal when dismissing a ui-bootstrap modal

Inside my ui-bootstrap modal controller, I have a $watch function set up for a variable. The code snippet looks like this: main.controller('modalCtrl', ['$scope', '$rootScope', '$modalInstance', function ($sc ...

The setting of the custom user agent in the Chrome Extension Manifest Version 3 is not functioning correctly

We currently have an extension that consists of only two files: manifest.json and background.js Despite the browser (Chrome version 112) not reporting any errors, we are facing an issue where the user agent is not being set to 'my-custom-user-agent&a ...

`Incorporating dynamic link filtering in vis.js`

Can links and nodes be filtered in a vis.js network? I have configured a DataSet for both nodes and edges as follows: function drawNetwork(container){ var nodes = new vis.DataSet(); populateNodes(nodes); // implementation details skipped ...

Create a dynamic sitemap for a Laravel website that includes variables in the slugs

My Laravel-based website features URLs like this- xyz.com/search/{id}/{name} These URLs have two variables, allowing for multiple variations such as: xyz.com/search/1/katy xyz.com/search/2/john With potentially thousands of such URLs, I need to creat ...