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

Extract data from JSON object

My JSON object is called idAndNames.json; [ { "id":"1", "name":"name1"}, { "id":"2", "name":"name2"}, { "id":"3", "name":"name3"} ] I'm looking to filter it by ID and name function applyFilter(id, valueItem) { return id <= valueIte ...

Improve efficiency by utilizing Delegates to streamline multiple if-else if statements

I developed a method to choose multiple filters based on true/false conditions. Here is the code snippet I implemented for this purpose: public T SetPropertyTypes<T>(bool residential, bool commercial) where T : IPage, new() { // Logic for Reside ...

Attempting to use `deleteAllCookies` or `cleanCookie` with InternetExplorerDriver may result in an Unreachable

Recently, I created a small test suite using geb that runs smoothly on Chrome and Firefox. However, when I tried to include Internet Explorer in the testing browsers, I encountered an issue with leftover cookies from previous tests causing failures (I rema ...

turning off next.js server side rendering in order to avoid potential window is undefined issues

I am currently managing a private NPM package that is utilized in my Next.js project, both of which are React and Typescript based. Recently, I integrated a graph feature into the NPM package and encountered an issue where any reference to window within t ...

Gather input from a form and store it in an Object upon submission

I am facing a challenge as I do not have much experience with object-oriented JavaScript. My goal is to have a form submit details such as the first and last name to an object that I've created. Here is the object I have: function Person(firstName, ...

What could be the issue with this particular Ajax script?

I am facing an issue with this JavaScript code - it runs smoothly in Chrome and IE, but encounters a problem in Firefox. I need assistance in identifying the issue. Can anyone help? <script> function checkGurantee() { var gurantee = document.get ...

Change the left position of the sliding menu in real-time

I am currently designing a website with a sliding menu feature. By default, the menu is set to -370px on the left, displaying only the "MENU" text initially. When a user hovers over the menu, it expands to the right, allowing them to select different menu ...

Modify the structure of the JSON string

My JSON string is structured like this: [ { "queryResult": { "A": "12-04-2014", "B": 1 } }, { "queryResult": { "A": "13-04-2014", "B": 2 } }, { "qu ...

What could be causing the issue with setting a value for a JSON array in the following method?

Consider this piece of code that I'm currently working with: compareList[productName] = productID + ',' + productHref; console.log(productName + ' ' + productID + ' ' + productHref + ' ' + compareList.length); ...

What's better: using like() in Perl Selenium or $sel->like()?

In addition to the standard methods of Test::More, Perl-Selenium also offers these methods as object methods. For example, you can use $sel->like() where $sel is the selenium perl object. The question arises - should these object methods be used at all ...

Error: The function 'myAppController' is not defined and is therefore not a valid argument

I am trying to incorporate my service into my controller and print a message to the console (in the Auth Service), but I keep encountering this error: Argument 'myAppController' is not a function, got undefined. Can you help me figure out what I& ...

JavaScript function unable to execute form action properly

I have a link RESET YEAR which triggers a servlet to check if the current year is equal to the present year. If they are not equal, then the function resetyear() is supposed to be called. The issue I am facing is that the function is not working as expecte ...

Can you explain the meaning of the code snippet provided?

<script type="text/javascript> window.onload = function(){ //execute some code here },function(){ // execute another piece of code here }(); </script> Is this code indicating that multiple function ...

Can React Router be integrated with Material UI tabs?

I have made some changes to the code of the Tabs component in material ui, <AppBar position="static"> <Tabs variant="fullWidth" value={value} onChange={handleChange} aria-label="nav tabs example" > < ...

Trigger an event in jQuery when the focus moves away from a set of controls

Within a div, I have three textboxes and am looking for a way to trigger an event when focus leaves one of these inputs without transitioning to another one of the three. If the user is editing any of the three controls, the event should not be triggered. ...

Ensure that the folder name contains specific characters

When working with AngularJS, I am developing a feature to create folders. One requirement is that if a folder name contains special characters like /, :, ?, "<", or |, an error message should be displayed stating "A folder name cannot contain any of the ...

Tips for securely transmitting data via a hidden form using the POST method

I have a query that displays records of people, and I want to send the id_Persona using a hidden form through a post function to the idsPersonas.php page. However, when I try to do this, it redirects me to an undefined page. Here is the code snippet: < ...

Alternative method for displaying text in Discord

At the moment, my discord bot is set up to read from a file and post the new data in that file to a specific channel. I am interested in modifying the output so that any characters enclosed in ~~ are displayed as strikethrough text. const Bot = require( ...

Tips for transferring an array variable into a div element using AJAX

I have a div like the following: <div id="#myid"> if($values){ echo "<p>$values['one']</p>"; echo "<p>$values['two']</p>"; } </div> Due to the large size of my div, I want to make a request ...

You can only use the angularjs http function once

After browsing through similar forum posts, I was unable to find a solution to my issue. It could be due to my limited experience with JavaScript and Angular. Here's the problem: Desired Outcome: When I click a button, I want the data from the server ...