The easiest way to implement what you need is to create a boolean method that checks if element exist (visible) on the page:
public static boolean CheckIfElementVisible(By by){
var _wait = new WebDriverWait(YourWebDriverInstance, TimeSpan.FromSeconds(30));
try
{
_wait.Until(ExpectedConditions.ElementIsVisible(by));
System.Diagnostics.Debug.Write("Element" + " " + @by + " " + "is visible");
}
catch (Exception e)
{
System.Diagnostics.Debug.Write("Element" + " " + @by + " " + "is not found |" + " " + e.Message);
}
}
}
Explanation of the piece of code:
by
- your element identifier (ID, CSS, Xpath and etc.)
TimeSpan.FromSeconds(30)
- time to wait for an element. I usually set to 30 seconds
try / catch block
- this way your test won't fail if element is not found.
This will either return true if element exist and then you can interact with it, or either returns false and you can skip to the next step. Hope this helps.