In my current project, I am using selenium-webdriver with Mocha & Chai in a JavaScript environment. I am trying to determine if a user is logged in by checking for the presence of a specific element in the DOM.
If the user is logged in, then the following element exists:
<a class="account-panel-controls__link" role="link" id="sign-out-nav" data-log-out="">Sign out</a>
I need to find this element and click on it if present. If not, I want to click on another element that only appears when the previous one is absent:
<a class="account-panel-controls__link" role="link" id="registration-sign-in-nav" data-log-out="">Sign in</a>
I have tried various approaches, but none seem to work reliably. The code I currently have works fine when the element with Id 'sign-out-nav' is initially present, but fails if it is not found, resulting in a test failure due to being unable to locate the element in the DOM at all.
driver.findElement(By.id('sign-out-nav')).isDisplayed().then(function (displayed) {
if (displayed) {
driver.findElement(By.id('sign-out-nav')).click();
}
else{
driver.findElement(By.id('registration-sign-in-nav')).click();
}
});
I have searched through multiple resources, attempting solutions such as .getSize() and checking for length, but I cannot get any of those methods to work. Using 'findElements' along with .length also returns an [Object object] value with a length of 1, making it difficult to distinguish the values.
Is there something crucial that I am overlooking?
Thank you