You can use the Exists method to determine if an object exists in the application under test.
The following code checks if a hyperlink with the text Log out exists on a Web page:
VB
If (browserWindow.Exists( "//a[@textContents='Log out']" )) Then ' do something End If
C#
if (browserWindow.Exists( "//a[@textContents='Log out']" )){ // do something }
You can use the Find method and the FindOptions method to check if an object, which you want to use later, exists.
The following code searches for a window and closes the window if the window is found:
VB
Dim mainWindow As Window mainWindow = _desktop.Find("//Window[@caption='My Window']", New FindOptions(False)) If (mainWindow IsNot Nothing) Then mainWindow.CloseSynchron() End If
C#
Window mainWindow = _desktop.Find<Window>("//Window[@caption='My Window']", new FindOptions(false)); if (mainWindow != null){ mainWindow.CloseSynchron(); }