【问题标题】:.Net Standard library using selenium/cefsharp.Net 标准库使用 selenium/cefsharp
【发布时间】:2020-09-14 21:28:23
【问题描述】:

我创建了一个 Windows 窗体机器人,它可以登录和浏览网页、获取一些数据并单击一些按钮。到目前为止,我一直在使用 cefsharp 和 cefsharp.winforms 在应用程序中显示浏览器。我想将此功能移植到 .net 标准库,拥有可以与机器人通信并将数据发送到某些前端的 aspnet 核心后端(基本上我想将 winforms 移植到 Web 应用程序)。 我想要一个 .net 标准库,以便 aspnet 核心和 win 表单可以使用相同的核心库。 据我所知,cefsharp.offscreen 不适用于 .net 标准。

我检查了 nuget.org 上的 Selenium.WebDriver 数据包并在找到的依赖项下

.NETStandard 2.0
Newtonsoft.Json (>= 12.0.2)

据我了解,这意味着它应该支持 .net 标准 2.0。但是,当我尝试从 .net 标准 2.0 库中使用 Windows 窗体 4.6.1 中的 Selenium.WebDriver 和 ChromeDriver 包的类创建对象时,我收到运行时错误

'Could not load file or assembly 'WebDriver, Version=3.141.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified

来自 WinForms 4.6.1 应用程序的代码:

public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    var test = new StandardLib.TestingWebDriver();
    test.TestDriver();
}

来自我的 StardardLib .net 标准 2.0 库项目的代码:

public class TestingWebDriver
{
    public void TestDriver() {
        var driver = new ChromeDriver();
        driver.Navigate().GoToUrl("https://google.com");
        Console.WriteLine(driver.PageSource);
    }
}

我一整天都在尝试不同的方法来解决这个问题,但没有成功。如果您知道如何解决这个问题或我可以使用任何替代方案来代替 cefsharp/selenium,我将非常感激 :)

【问题讨论】:

  • 我之前没有使用 C# 来处理 Selenium,但部分问题可能是使用 .net 标准。微软的“.net”术语非常非常混乱……见这个链接:infoq.com/news/2017/10/dotnet-core-standard-difference
  • 另见:docs.microsoft.com/en-us/dotnet/standard/… 值得注意的是,.net core 包含和/或与大多数 .net 框架库兼容(因此 .net core 可以导入几乎任何 .net 框架库并且将在某个时候将它们全部包含在内...)您也可以考虑使用 webview 或嵌入的 IE 让服务器提供界面...
  • 我已经阅读了这两个链接,但仍然感到困惑。在为 .net 标准库和 .net 框架为 winforms 选择“正确”版本时,它可以毫无问题地编译。我关注docs.microsoft.com/en-us/dotnet/standard/net-standard 的表格。如果我理解正确,我可以制作一个 .net 框架 lib 项目,winforms 和 asp.net 核心应用程序都可以使用它吗?试试看,谢谢!

标签: c# .net selenium standards cefsharp


【解决方案1】:

终于明白了。因此,winforms 应用程序试图在 winforms /bin/debug 文件夹而不是库的 /bin/debug 中查找(seleniums)webdriver.dll。

我所要做的就是将标准库中的相同软件包也安装到 winforms 中,它就可以工作了!

我认为这是预期的工作流程(出于某种我不明白的原因),有人可以详细说明。所以毕竟它与.net标准无关,只是我不知道如何正确使用/导入类库。

【讨论】:

    猜你喜欢
    • 2016-11-08
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 2017-09-18
    • 2020-09-21
    • 1970-01-01
    相关资源
    最近更新 更多