I am trying to create a simple Selenium script to login to a page directly proceeded by logging out from the page. I am using Selenium with C# and PageFactory to reach the elements on the webpage.

I am able to login to the page, but unable to logout. The login and logout button themselves are made visible by clicking the loginLogoutElementMakeVisible element seen below. This element changes after logging in.

My current solution is to have one reference to the element in my code as seen below:

[Findsby(How = How.CssSelector, Using = "a[href='javascript:void(0)']")] public IWebElement loginLogoutElementMakeVisible

Html for the button to make login button visible:

<li class="nav-link sm-menu-item more">
<a data-bind="click: function(){ loginClicked(true); }, text: translate('menu_login')" href="javascript:void(0)">Login</a>

Html for the button to make logout button visible:

<li class="nav-link sm-menu-item more nav-profile">
    <a href="javascript:void(0)">
        <img data-bind="attr: { 'src': avatarUrl }" src="https://media.licdn.com/mpr/mpr/shrink_200_200/p/1/000/1c2/3a6/04b3e5c.jpg">
    <ul class="submenu sm-submenu">

The general concept I have used is to have these two action methods in the page object to login and logout.

public void Login()
    loginExecuteButton.Click(); //Now logging in
    Browser.WaitForPageReload() //Own code in Browser class see below (1)

public void Logout()
    loginLogoutElementMakeVisible.Click(); //Never clicked in test
    logoutExecuteButton.Click() //Now logging out

In Browser class (1) ->
public void WaitForPageReload()
    IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));

    wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));

I have also tried creating two separate fieldmember WebElements (e.g. one loginElementMakeVisible and one logoutElementMakeVisible) for the button to display the login and the logout button, but still having them in the same page object and thus initializing them at the same time (Thought this could work since pagefactory uses lazy loading)

I have also tried to have the logout button in another pageobject which is initialized after login in the test.

Is there a way to make the pagefactory update its proxy to the loginLogoutElementMakeVisible so I can logout? Or am I still trying to logout before the page after logging has had a chance to load? Are there other problems which could explain why I cannot click the MakeVisible element?

1 Answer 1



No need to declare/initialize two different WebElements for this. I would suggest solution -

  • After login.click()
  • Use method WaitForPageReload() [Please confirm whether your code really working and waiting till page get loaded completely]
  • Once Page get loaded, Assert for AnyField [Which should be uniquely displayed only after successful login]
  • Now, Assert "login.getText()" and expected "logout button text"
  • Once above assert passes, our work is done
  • Part remain only is - login.click to logout

Lets see how its working at your end !

  • The webpage has changed since this post so the workflow is different. Your list seems like a prudent checklist. Some notes: The WaitForPageReload seems to work OK when there is a full page reload. However, I would suggest not using that solution and instead wait for a specific element to become visible (Use webdriver wait until with an expected condition). Stuff can still happen after a document gives readystate. The main problem of this post I think has been about how to wait, to ensure the workflow is stable. Using the webdriver wait is a robust way of waiting.
    – Jakob
    Commented Jul 4, 2016 at 7:53
  • Agree with you @Jakob. Your notes are helpful. Have you resolved your issue? Commented Jul 6, 2016 at 6:25
  • Yes it works as it should, good feedback @Narendra and loginLogoutMakeVisible is declared once. Note to readers and myself: Find elements through locators that are unique, descriptive and unlikely to change. My mistake was to break the first rule. Although the ...MakeVisible element was unique when logging in it wasn't when having logged in. Changing the locator and fixing the wait I believe is what resolved my issue.
    – Jakob
    Commented Jul 6, 2016 at 7:35
  • Great ! When you got solution, objectives fulfilled :) @Jakob - Noted your good habit : You have shared your experience with do's and don'ts. If you found my answer helpful, can you accept it? Commented Jul 6, 2016 at 8:57
  • Yes of course! See you out there!
    – Jakob
    Commented Jul 6, 2016 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.