question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Couldn't click on "Blank workbook" button after launching MS Excel

See original GitHub issue

Hi,

I’m trying to do a sample workflow on Microsoft Excel 2016.

Workflow: 1.Launch MS Excel. 2. Click on “Blank workbook” to create a blank document. 3. Click on “Insert” tab.

Issue : MS Excel getting launched but couldn’t click on “Blank workbook” button and test getting failed at below screen and it remains (Excel not getting close) ; image

When I re-run the test for the second time, the test begins from the existing screen (above screen) and second time “Blank workbook” button getting clicked and test continuous and run successfully.

My code snippet :

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import io.appium.java_client.ios.IOSDriver;

public class ExcelISDPOC {
	
    private static IOSDriver Excel;
  
    @Test
    public void test() throws InterruptedException {
		
	DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("app", "C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\EXCEL.EXE");
        Excel = new IOSDriver (new URL("http://127.0.0.1:4723"), cap);
//        Excel.manage().window().maximize();
        Excel.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);   	

    Thread.sleep(3000);
    Excel.findElementByName("Blank workbook").click();
    Thread.sleep(3000);	
    Excel.findElementByName("Insert").click();
    Thread.sleep(3000);    
    }	
}

Console :

ERROR FAILED CAUSE:
org.openqa.selenium.NoSuchWindowException: Currently selected window has been closed (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 52 milliseconds
Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
System info: host: 'LKCONVSELVARA01', ip: '192.168.8.101', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: io.appium.java_client.ios.IOSDriver
Capabilities [{app=C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE, platformName=iOS, platform=ANY}]
Session ID: EA27B09A-E89F-41A0-96FC-A320C90639FA
*** Element info: {Using=name, value=Blank workbook}
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_131]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_131]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_131]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_131]

WinAppDriver log :


==========================================
POST /session/EA27B09A-E89F-41A0-96FC-A320C90639FA/element HTTP/1.1
Accept-Encoding: gzip,deflate
Connection: Keep-Alive
Content-Length: 41
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:3073
User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_131)

{"using":"name","value":"Blank workbook"}
HTTP/1.1 400 Bad Request
Content-Length: 102
Content-Type: application/json

{"status":23,"value":{"error":"no such window","message":"Currently selected window has been closed"}}

Could you please help me to solve this problem. cc: @yodurr

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
timotiusmargocommented, Aug 17, 2017

Hi @vijay44,

The first error you encounter is due to the way Microsoft Excel launches. When Excel is not yet launched and you create a new session to launch it, Excel will start few processes and potentially show multiple windows like a splash screen. At this point, Windows Application Driver takes the first valid application window (the splash screen) and treat it as the main window to interact with. As soon as the splash screen is dismissed, any subsequent commands you send to it will result in no such window error which you saw.

When you wait enough time until the main window containing the Blank workbook is displayed and invoke launchApp() API, WinAppDriver will re-launch the application based on the initial information (capabilities) you provided during session creation. In the case of your version of Microsoft Excel configured the way it was, it simply highlighted existing instance instead of creating a new instance. As a result, the main window that is now displayed is set as the main window as it is the first valid main window at this point belonging to the process.

Now that we have the right explanation of what happened, you can use it as a working workaround as long as you configured your Microsoft Excel the way it was. In fact this is a very good use of the launchApp() in this particular condition. Alternatively, you can also use SwitchTo() API to switch to the right main window once the correct main window is displayed. Below is a sample on how to switch window if your session is pointing to a wrong window such as the splash screen:

DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("app", YourAppId);
WindowsDriver<WindowsElement> session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
Assert.IsNotNull(session);

// Identify the current window handle. You can check through inspect.exe which window this is.
var currentWindowHandle = session.CurrentWindowHandle;

// Wait for 5 seconds or however long it is needed for the right window to appear/for the splash screen to be dismissed
Thread.Sleep(TimeSpan.FromSeconds(5));

// Return all window handles associated with this process/application.
// At this point hopefully you have one to pick from. Otherwise you can
// simply iterate through them to identify the one you want.
var allWindowHandles = session.WindowHandles;

// Assuming you only have only one window entry in allWindowHandles and it is in fact the correct one,
// switch the session to that window as follows. You can repeat this logic with any top window with the same
// process id (any entry of allWindowHandles)
session.SwitchTo().Window(allWindowHandles[0]);

For the benefit of others, below is the snippet of your workaround that will work if the application under test enforce single instance and will simply bring up already launched instance when being re-launched.

DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("app", YourAppId);
WindowsDriver<WindowsElement> session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
Assert.IsNotNull(session);

// Wait for 5 seconds or however long it is needed for the right window to appear/for the splash screen to be dismissed
Thread.Sleep(TimeSpan.FromSeconds(5));

// When the application uses pre-launched existing instance, re-launching the application simply update 
// the current application window to whatever current main window belonging to the same application 
// process id
session.LaunchApp();
1reaction
vijay44commented, Aug 17, 2017

Thanks @timotiusmargo. The above explanation is more helpful and interesting .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Excel opens a blank screen when you double-click a file ...
Offers resolutions to an issue where you see a blank screen when you try to open an Excel workbook by double-clicking its icon...
Read more >
Repair a corrupted workbook
Recover data when you can't open the workbook in Excel ; Click File > New. ; Under New, click Blank workbook. ; Click...
Read more >
Repairing a corrupted workbook
Click the arrow next to the Open button, and then click Open and Repair. Do one of the following: To recover as much...
Read more >
Open Excel document but no worksheet is displayed.
To open Excel in application safe, click start > All Programs > Microsoft Office > Press and hold the Ctrl key and click...
Read more >
Options won't open in files on Excel
1) Close all open Excel files. · 2) Go to `Start` and type `Excel` in the search box. · 3) Click on the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found