When switching back to the parent window and attempting to execute an action, a JavaScript error is encountered

Currently automating a SnapDeal eCommerce website

A challenge I am facing is an error message in the console:

The issue occurs when transitioning from a child window back to the parent window and performing operations on the parent window.

    //Automating the login process on SnapDeal using Facebook.

    SnapDeal.findElement(By.xpath("//div[@id='fbUserLogin']")).click();
    String pWindow = SnapDeal.getWindowHandle();
    for (String winHandle : SnapDeal.getWindowHandles()) 
    {
           //Switching to the child window 
           SnapDeal.switchTo().window(winHandle);
    }

    WebElement FBLoginID = SnapDeal.findElement(By.xpath("//input[@id='email']"));
    JavascriptExecutor js = (JavascriptExecutor)SnapDeal;
    js.executeScript("arguments[0].value='YourEmailID';", FBLoginID);

    WebElement FBPass = SnapDeal.findElement(By.xpath("//input[@id='pass']"));
    JavascriptExecutor jE = (JavascriptExecutor)SnapDeal;
    jE.executeScript("arguments[0].value='YourPassword';", FBPass);
    SnapDeal.findElement(By.xpath("//button[@id='loginbutton']")).click();
    SnapDeal.findElement(By.xpath("//button[@name='__CONFIRM__']")).click();
    Thread.sleep(5000);

    //Returning to the parent window
    SnapDeal.switchTo().window(pWindow);

    //Logging out of the application on the parent window after returning 
    WebElement WelCome = SnapDeal.findElement(By.xpath("//span[contains(.,'Welcome')]"));
    WebElement LogOut = SnapDeal.findElement(By.xpath("//a[contains(.,'Logout')]"));
    Actions mousehover = new Actions(SnapDeal);
    mousehover.moveToElement(WelCome).perform();
    Thread.sleep(5000);
    mousehover.click(LogOut).perform();

If you have any insights on how to address this problem, please let me know. Thank you.

Answer №1

Start by storing the parent and child windows using SET, then proceed to iterate through them. While I have not personally tested your code, this approach of utilizing SET and an iterator should be effective for your needs.

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

Acquire information asynchronously in JavaScript while inside a for loop

Recently, I've been exploring this particular function snippet: function add_cnh(arr_clelem){ var id=arr_clelem.getElementsByClassName("present")[0].id; var date=arr_clelem.getElementsByClassName("present")[0].getAttribute('date'); v ...

Searching for the position of objects within a collection?

I am seeking the ability to parse through an object and allocate each of its available attributes to a variable. Within this scenario, there are four potential properties present in different objects - some containing all four while others may only have t ...

Unable to authenticate through LinkedIn

On my website, I am attempting to retrieve the user's first name, last name, and email using LinkedIn. Here is what I have done so far: In my LinkedIn App settings, under the Default Scope (OAuth User Agreement), I have selected the following options ...

How come despite installing node 10.1.0, the system is showing the installed version as 5.6.0?

After I downloaded the NPM Windows installer from this link: https://nodejs.org/it/, I made sure to download and install the 10.1.0 version. However, when I checked my console using the command node -v, I was surprised by the following output: C:\U ...

How can I retrieve a PDF from a URL provided by the server using AngularJS?

Is it possible to automatically download a PDF file from a URL received through a $http GET() request from the server? The $http request is triggered by clicking a button on the front-end, and upon successful completion of the request, the URL where the PD ...

Transform two fixed elements into dynamic elements using JQuery

Is there a way to replace two static elements in the table <tr class="item-row"> with data from an ajax call instead of just appending to them? Currently, I am using this javascript line to append: $(".item-row:last").after('<tr class="item- ...

Establish a table containing rows derived from an object

I am currently dealing with a challenge in creating a table that contains an array of nested objects. The array I have follows this schema: array = [ { id: 'Column1', rows: { row1: 'A', row2 ...

Is there a way to preserve line breaks when programmatically copying text to the clipboard with JavaScript?

Utilizing Javascript alongside a duo of devexpress asp.net controls to dynamically duplicate the contents of an ASPxMemo (multiline textfield) and assign those contents as the body of an email. The copying and pasting is functioning correctly for the most ...

How can I access the result of a getJSON promise within the $.when method?

I am working on a piece of code where I aim to collect data from 2 getjson calls and store it in an array only when both calls have successfully completed. However, I encountered the following error: resultFromUrl1.feed is undefined var entry1 = resultFro ...

Guide on deleting an item from an object array in AngularJS

I have an array of objects and need to delete a specific object from it: var objectArray = [{"id":"John Doe","label":"John Doe","shape":"image","image":"app/data/img/user_icon.png","color":{"background":"#db630d","border":"#7c3400"},"level":0},{"id":"Java ...

I am struggling to extract data from an ASP site once I reach page 10

The target website I am currently scraping is: My code can successfully navigate up to page 10 by analyzing pagination numbers and iterating over them. However, when attempting to go beyond page 10, it encounters an issue due to the presence of three dots ...

How to insert space after every item generated by ng-repeat in AngularJS

I'm looking to evenly distribute elements based on this inquiry How to distribute li elements equally? I am utilizing angular with jade: ul li(ng-repeat="item in items") {{item.name}} However, ng-repeat does not create newlines after each element ...

What is the process for creating and registering custom Handlebars functions?

Despite spending plenty of time searching, I am still unable to find detailed information on where exactly to place my custom handlebars helpers. Should they be added in a <script> tag within my webpage's .hbs file? Or should I include them in a ...

Tooltips experience issues when interacting with elements that do not utilize the :active state

$(function () { $('[data-toggle="tooltip"]').tooltip() }) <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" ...

"Exploring the versatility of using variables in jquery's .css

I’m facing an issue where I need the rotation of a div to be based on the value stored in "anVar." This is what I currently have: function something() { $('.class').css('-webkit-transform:rotate('anVar'deg)') } The desi ...

Placing a user's username within an ejs template using express and node.js

Currently, I am attempting to integrate the username into a layout using ejs templating with node and express. Below are the steps I have taken: Mongodb model: const mongoose = require('mongoose') const Schema = mongoose.Schema; var uniqueValid ...

Struggling with rendering an HTML element utilizing jQuery's attribute functionality, encountering issues exclusively in Internet Explorer version

I need assistance with generating and inserting an HTML element using jQuery. In my code, I am including a class attribute for the element as shown below: jQuery('<li></li>', { class: "myClass" }); However, when testing in IE ...

Bringing together Events and Promises in JS/Node is the key to seamless programming

I am exploring the most effective approach to incorporating events (such as Node's EventEmitter) and promises within a single system. The concept of decoupled components communicating loosely through a central event hub intrigues me. When one componen ...

Having difficulty removing new or existing lines on StackBlitz

I attempted to experiment with React on StackBlitz, but I encountered a problem where I couldn't delete any lines of code. It seems that while I can add new lines of code, deleting them is not an option. Even when logging in with GitHub, the issue per ...

Uploading Files Using Ajax to a PHP Class

Having some trouble with my file upload using AJAX to post to an object-oriented PHP class. Here's the AJAX code: var handleUpload = function(event) { event.preventDefault(); event.stopPropagation(); var fileInput = document.getElement ...