0

I want to update my legacy project selenium library to newest. Unfortunately I am facing problems with PageFactory.InitElements method (which is by default unavailable in Selenium 4, but available in DotNetSeleniumExtras.PageObjects). This is my example code:

class Program
{
    static void Main(string[] args)
    {
        var options = new FirefoxOptions();
        options.BrowserExecutableLocation = @"C:\Program Files\Mozilla Firefox\firefox.exe";

        var firefoxDriverService = FirefoxDriverService.CreateDefaultService("d:\\gecko");
        firefoxDriverService.HideCommandPromptWindow = true;

        var driver = new FirefoxDriver(firefoxDriverService, options);
        driver.Navigate().GoToUrl("SITE_URL");

        var loginPage = new LoginPage(driver);
        loginPage.EnterLogin("foo");
    }
}

public class LoginPage
{
    public IWebDriver Driver { get; private set; }

    [FindsBy(How = How.XPath, Using = "//input[contains(@id,'login_btn')]")]
    public IWebElement LoginTextField { get; protected set; }

    public LoginPage(IWebDriver driver)
    {
        Driver = driver;
        PageFactory.InitElements(Driver, this);
    }

    public void EnterLogin(string login)
    {
        LoginTextField.SendKeys(login);
    }
}

If I run this, I am getting error on PageFactory.InitElements line:

System.TypeLoadException: Could not load type 'OpenQA.Selenium.Internal.IWrapsElement' from assembly 'WebDriver, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null'

If I change packages configuration from:

DotNetSeleniumExtras.PageObjects 3.11.0
Selenium.WebDriver 4.1.0

to:

DotNetSeleniumExtras.PageObjects 3.11.0
Selenium.WebDriver 3.141.0

It works fine. Seems like a new version of Selenium doesn't like legacy PageFactory.InitElements. Is there anything I can do about it? I would like to have my page object classess as they are right now, because there are many of them.

dafie
  • 951
  • 7
  • 25

2 Answers2

1

you cannot use PageFactory class and FindsBy attribute to store element when you use selenium version 4.1.0, instead create as a property for IWebElement/By type and it resolves this issue,I hope this solves your issue. Link to similar issue

Manikandan
  • 11
  • 2
  • 3
  • Actually you can. This helped: https://github.com/DotNetSeleniumTools/DotNetSeleniumExtras/pull/11 – dafie Feb 05 '22 at 21:31
  • I could able use the pagefactory by adding the package DotNetSeleniumExtras.PageObjects.Core V4.0.1, along with DotNetSeleniumExtras.PageObjects V 3.11.0 and selenium packages version 4.1.0, I guess using the above packages would have solved. – Manikandan Feb 13 '22 at 17:21
0

maybe this will solve your problem. The import is as follows:


import org.openqa.selenium.support.PageFactory;

And Page Factory initialisation is as follows (I have configured it so you can just copy/paste it) :

public LoginPage (){
        PageFactory.initElements(driver, this);
    }
Funky Monkey
  • 121
  • 5