10

I'm creating a test automation framework in c#. At the top of the project I have "using OpenQA.Selenium.Support.PageObjects;", however when I try to reference PageFactory.initElements(driver, PageName) I'm seeing the message "The name 'PageFactory' does not exist in the current context".

I thought the PageFactory class was included as part of the Selenium.Support nu get package. Tutorials online seem to reference the PageFactory in the same way I am with no extra imports, is there anything I'm missing?

NuGet packages:

  • Using Selenium.WebDriver version 3.9.1
  • Using Selenium.Support version 3.9.1
  • Using NUnit 3.9.0
  • Using NUnit3TestAdapter 3.9.0
  • Microsoft.NET.Test.Sdk 15.5.0
  • Code below:

    using NUnit.Framework;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Support.PageObjects;
    using System;
    using System.IO;
    
    namespace Framework.TestCases
    {
        class TestCase
        {
            [Test]
            public void Test()
            {
                String pathToDrivers = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\Drivers";
                IWebDriver driver = new ChromeDriver(pathToDrivers);
                String searchTerm = "Search Term";
    
                driver.Url = "https://website.com/";
                driver.Manage().Window.Maximize();
    
                HomePage homePage = new HomePage(driver);
                PageFactory.initElements(driver, homePage);
            }
        }
    }
    
    Brad White
    • 121
    • 1
    • 1
    • 7

    8 Answers8

    7

    In case anyone else comes across this question, reason why you can't find the PageFactory nowadays is pretty simple: It does not exist.

    Namely, with 3.11.0 release of Selenium.Support, PageFactory and ExpectedConditions were marked as obsolete. With Selenium.Support 3.12.0 they have been removed completely. More on that topic here.

    Solution to this is to simply add DotNetSeleniumExtras to your packages as those were moved to separate repository. One can also find useful Dreamescaper's fork (NuGet) that has added .NET Core support until the original repo finds an owner.

    sun_of_faith
    • 71
    • 1
    • 4
    6

    If anyone faces the same issue, please install from NuGet package manager the following: DotNetSeleniumExtras.PageObjects.Core (3.12.0)

    Carles
    • 2,731
    • 14
    • 25
    Seema
    • 61
    • 1
    • 1
    • After adding `DotNetSeleniumExtras.PageObjects.Core` you can use `PageFactory.InitElement(driver, this)` by importing `using SeleniumExtras.PageObjects;` – Suban Dhyako Jun 07 '20 at 04:56
    2

    I eventually just created a new project, and ported everything over with one difference, the type of project I created was a Visual C# > Test > Unit Test Project, before the project I had created was Visual C# > .Net Core > Class Library project (I was following a tutorial).

    I'm not too sure if this changed anything as such or just meant I had a clear project when I re downloaded and installed the NuGet packages, however I can now access the PageFactory class and associated methods. Thanks to everyone who replied.

    Brad White
    • 121
    • 1
    • 1
    • 7
    2

    small update for all people who have the same problem.

    : https://github.com/SeleniumHQ/selenium/issues/4387

    " Further discussion on this issue will not change the decision on the removal of the .NET PageFactory implementation "

    1

    Had the same issue, tried to uninstall and re-install but it did not work. Then I went looking for NuGet packages with Selenium in the name and found that the Selenium.Support package is separate from the main Selenium.Webdriver package. So if you're having this issue, go back and make sure you've installed both Selenium.Webdriver and Selenium.Support packages through Nuget.

    1

    PageFactory is still available

    Reference packages

    <PackageReference Include="DotNetSeleniumExtras.PageObjects" Version="3.11.0" />
    <PackageReference Include="DotNetSeleniumExtras.PageObjects.Core" Version="3.12.0" />
    

    Then

    using SeleniumExtras.PageObjects;
    
     HomePage loginPage = new HomePage(driver);
     PageFactory.InitElements(driver, HomePage)
    

    PageFactory.InitElements is void.

    M.Hassan
    • 10,282
    • 5
    • 65
    • 84
    0

    As you have mentioned that when you _try to reference PageFactory.initElements(driver, PageName), you are seeing an error message as follows :

    The name 'PageFactory' does not exist in the current context". 
    

    If you look at the API Documentation of PageFactory Class it clearly mentions the following :

    • System.Object : OpenQA.Selenium.Support.PageObjects.PageFactory
    • Namespace : OpenQA.Selenium.Support.PageObjects

    So the error essentially means that there are some issues with the installation of the Selenium.Support.PageObjects package.

    Solution

    A proper solution would be to completely Uninstall the Selenium.WebDriver and Selenium.Support.PageObjects related packages and then reinstalling them back through the NuGet Manager .

    Note : You can find a similar discussion here.


    Update

    As you are still seeing the same error can you make a small change in your code as follows :

    Replace :

    HomePage homePage = new HomePage(driver);
    PageFactory.initElements(driver, homePage);
    

    With :

    HomePage homePage = PageFactory.initElements(driver, HomePage);
    
    undetected Selenium
    • 183,867
    • 41
    • 278
    • 352
    0
    1. Install from NuGet package: DotNetSeleniumExtras.PageObjects.Core

    2. You can use PageFactory.InitElements(driver, this);