Asp.net 核心:Selenium 在当前目录中没有看到 geckodriver

Asp.net core: Selenium doesn't see geckodriver in current directory

web api 核心 2.0 项目没有看到 geckodriver.exe 我该如何解决?!

我创建了一个 asp.net 核心 2.0 web api 项目。 我将 geckodriver.exe 放入项目目录,我将 "Copy to output directory" 更改为 "Copy always"。

这是我的控制器:

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace WebApplication1.Controllers
{
    [Route("api/[controller]")]
    public class ValuesController : Controller
    {
        // GET api/values
        [HttpGet]
        public IEnumerable<string> Get()
        {
            if (!System.IO.File.Exists("geckodriver.exe"))
            {
               throw new Exception("error"); 
            }

            using (IWebDriver driver = new FirefoxDriver())
            {
                driver.Navigate().GoToUrl("http://gooogle.com");
            }
            return new string[] { "value1", "value2" };
        }
    }
}

当我开始我的项目时,我收到异常消息

OpenQA.Selenium.DriverServiceNotFoundException: 'The geckodriver.exe file does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at https://github.com/mozilla/geckodriver/releases.'

我想知道为什么使用相同代码的 .net framework mvc 应用程序运行良好!

我的问题是:如何解决我的问题?我在 google

上找不到解决方案

复制driver.exe到以下路径:

C:\Users\[userName]\.nuget\packages\selenium.webdriver.6.0\lib\netstandard2.0\

驱动程序被复制到输出目录。只需使用以下代码行: var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))

发生这种情况是因为 .NET Core 项目的 NuGet 包是从全局位置而不是 .NET Framework 项目的包文件夹加载的。要修复它,我们需要指定执行文件夹的路径。 (来源:https://www.automatetheplanet.com/webdriver-dotnetcore2/