Forum Discussion

Ali2's avatar
Ali2
Occasional Contributor
10 days ago
Solved

I want to ignore unable to find object error, how do i do that??

function closetabs(){
var tabContainer = NameMapping.Sys.TRAFiX.HwndSource_MainWindow1.MainWindow1_.dockLayoutManager.mainlayoutGroup.mainDocumentGroup;

var totalTabs = 10; // Assuming there are 10 tabs
for (var i = 2; i <= totalTabs; i++) {
    var tab = tabContainer.WPFObject("DocumentPaneItem", "", i); // Adjust the index accordingly
    
    if(tab.Exists)  
    tab.Click(tab.Width-10,tab.Height/2)
    }
}

Here in my application there can be as low as 2 tabs and as many as 10 tab, now I want to close all the tabs that are there in my application.
Lets say there are 3 tabs and I close them through this code, as I have 3 tabs and I run this, I get this results, now I know there are no tab 4, 5 etc.

HOW DO DEAL WITH THIS?

 

  • I would suggest that you make updates to your script
    1.Remove the if Exists
    2.Replace the for loop with a while Exists loop (this also would eliminate the need to specify the number of tabs)

    Also the if Exists will wait for the default timeout of 10 seconds before proceeding, so you will need to change the default time out and restore it back when done.

    I have not tried this modified code but should give you the idea.

    function closetabs()
    {
      var tabContainer = NameMapping.Sys.TRAFiX.HwndSource_MainWindow1.MainWindow1_.dockLayoutManager.mainlayoutGroup.mainDocumentGroup;
    
      // Change default timeout from x seconds to one second
      var TimeoutValue;
      TimeoutValue = Options.Run.Timeout;
      Options.Run.Timeout = 1000;
      
      // Use while exists loop 
      var tab = tabContainer.WPFObject("DocumentPaneItem", "", 1); // Always use first tab
      while(tab.Exists == true)
        tab.Click(tab.Width-10,tab.Height/2);
        var tab = tabContainer.WPFObject("DocumentPaneItem", "", 1); // Always use first tab
      
      // Restore default timeout back to x seconds
      Options.Run.Timeout = TimeoutValue;
    }

     

  • rraghvani's avatar
    rraghvani
    Icon for Champion Level 3 rankChampion Level 3

    The code you have written, implies that the objects actually exists, and if it doesn't exist then TC will wait 10 seconds before continuing with the next step (10 seconds is based on the auto-wait timeout in Project Settings -> Playback).

    To make your code better, use the Find or FindChild method. For example,

    function closetabs() {
        // Use Aliases instead of long names
        var tabContainer = NameMapping.Sys.TRAFiX.HwndSource_MainWindow1.MainWindow1_.dockLayoutManager.mainlayoutGroup.mainDocumentGroup;
        
        for (var i = 2; i < tabContainer.ChildCount; i++) {
            var tab = tabContainer.FindChild("ObjectIdentifier", "WPFObject(\"DocumentPaneItem\", \"\", i)");
            if (tab.Exists) {
                tab.Close(); // Check if tab has Close method
            }
        }
    }

     

  • eykxas's avatar
    eykxas
    Regular Contributor

    Hi !

    Personally, I handle these kind of case with a code like that :

    Log.Enabled = false;
    Options.Run.Timeout = 50;
    let elementHTML = page.FindElement("//mat-progress-spinner[@role='progressbar']");
    Options.Run.Timeout = 2000;
    Log.Enabled = true;

    Most of the time waiting for the auto-wait timeout is way too long and it can be perfectly acceptable if my object doesn't exist. So I disable the log (to avoid any error) and just wait 50ms by setting the value in the code. (Remember to reset the value after).

  • Hassan_Ballan's avatar
    Hassan_Ballan
    Regular Contributor

    I would suggest that you make updates to your script
    1.Remove the if Exists
    2.Replace the for loop with a while Exists loop (this also would eliminate the need to specify the number of tabs)

    Also the if Exists will wait for the default timeout of 10 seconds before proceeding, so you will need to change the default time out and restore it back when done.

    I have not tried this modified code but should give you the idea.

    function closetabs()
    {
      var tabContainer = NameMapping.Sys.TRAFiX.HwndSource_MainWindow1.MainWindow1_.dockLayoutManager.mainlayoutGroup.mainDocumentGroup;
    
      // Change default timeout from x seconds to one second
      var TimeoutValue;
      TimeoutValue = Options.Run.Timeout;
      Options.Run.Timeout = 1000;
      
      // Use while exists loop 
      var tab = tabContainer.WPFObject("DocumentPaneItem", "", 1); // Always use first tab
      while(tab.Exists == true)
        tab.Click(tab.Width-10,tab.Height/2);
        var tab = tabContainer.WPFObject("DocumentPaneItem", "", 1); // Always use first tab
      
      // Restore default timeout back to x seconds
      Options.Run.Timeout = TimeoutValue;
    }

     

    • Ali2's avatar
      Ali2
      Occasional Contributor

      Thanks this worked for me