1

I cannot find elements that I want to interreact with on any page other than the main page. The electron app I am working with has several that load with the start of the app but I am only able to access the elements on the first page. I think there must be an error in how I switch pages because I am confident in how I am looking for elements. The segment of code that changes page and looks for element is below.

var mainhandle = await driver.getWindowHandle();          
var windowhandles =await driver.getAllWindowHandles();
var mainhandle = await driver.getWindowHandle();          
 
  
  windowhandles.forEach(async newwindow => {
    console.log(newwindow);
       if(newwindow != mainhandle){
        await driver.switchTo().window(newwindow);
        console.log("switching to window " + newwindow)
        var exists = await driver.findElements(By.css('#App > div > div.MuiPaper-root.MuiCard-root.jss10.MuiPaper-elevation1.MuiPaper-rounded > div.MuiCardHeader-root.jss14 > div.MuiCardHeader-action > button')).then(found => !!found.length);
        if(exists==true){
            console.log("ayyyyyyy we made it") ;
            const newtask = await driver.findElement(By.css('#App > div > div.MuiPaper-root.MuiCard-root.jss10.MuiPaper-elevation1.MuiPaper-rounded > div.MuiCardHeader-root.jss14 > div.MuiCardHeader-action > button'));
            newtask.click();
            exists=false
        }
        else{
          var testhandle = await driver.getWindowHandle();
          console.log("Element not found in " + testhandle);
        }
        // await driver.close();
      }
      else{
        console.log("this must be the main "+ mainhandle)  
  }
  
  }) 

Edit: I thought I should add that each "switching to window" test output shows a new window but the "element not found" always shows the last window handle. I'm not sure if this is just an issue with how I am testing and viewing my output or if there is something more to it.

Nic.C
  • 11
  • 2
  • Don't use forEach. Just reference the windowshandles set and iterate through a for loop. – Nic.C Jun 28 '21 at 16:42

1 Answers1

0

This format worked for me. Hope it helps someone else in the future :)

this.mainhandle = await driver.getWindowHandle();
var windowhandles =await driver.getAllWindowHandles();
for (let i = 0; i < windowhandles.length; i++) {
    await driver.switchTo().window(windowhandles[i]).then(() => console.log('switched to NewWindow handle ' + windowhandles[i]))
    await delay(100)
    if(await driver.getTitle().then(found => found == "Job Plan Manager")){
                    this.JPMhandle = await driver.getWindowHandle()
         //do the work
         break;
    }
}
Nic.C
  • 11
  • 2