Search code examples
c#seleniumselenium-webdriver.net-core.net-standard

Nuget - " Could not load file or assembly '{Class Name}, Version= x.x.x., Culture=neutral, PublicKeyToken=null' or one of its dependency"


I am creating a class Library using .NetStandard and I have downloaded the following from the Selenium package from the NuGet Store

enter image description here

Now I have a simple test code

using System;
using Operation_Forage_V2;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

public class test
{
    public static void Main(string[] args)
    {
        Uri Aurl = new Uri("https://www.youtube.com/");

        IWebDriver aDriver;

        aDriver = new ChromeDriver();

        aDriver.Url = Aurl.OriginalString;

        var element = aDriver.FindElement(By.ClassName("sa_wr"));
        var innerHtml = element.GetAttribute("innerHTML");

        Console.WriteLine(innerHtml.Substring(0, 100)); //just to see if it got the html

    }         
}

The problem is when I run the code (as console application), I get the following error

enter image description here

Ive seen similar problems on this websites, but they did not help me

I have no idea why this error is occurring especially given the driver and the right version of the driver has been downloaded using NuGet and the picture clearly shows it.


Solution

  • I don't believe this is an issue with .Net Standard, as I was able to get it loaded, but got a different error, as I have a more recent version of Chrome running, but I was able to load the assemblies fine using Class Lib (.Net Standard 2.0.3) and Console App (.Net Core 3.1).

    Screen shot of test project

    EDIT: Your output/bin folder should look something like the following:

    enter image description here

    ... and your .csproj file for your reference lib should have at least the follow: enter image description here

    Edit #2: I just noticed from your nuget packages screenshot above that you have warnings on them. Please delete remove you references and try adding them back, or if you right click your project is VS there should be an option to redownload your packages, but I never have much success with the ladder method. However it does seem to be an issue with you packages. If these don't work for you, please try removing your bin and obj folders and rebuild, sometimes this can help. If you still have issues, please share the ⚠️ messages as they might assist in debugging.

    Edit #3: You may also want to try and see if adding this <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> to you .csproj file as explained here might help: Nuget Pkg Dlls Missing from Build Folder in .Net Standard DLL Project. dlls are Not Copy Local? Fix in Visual Studio 2019?