我创建了一个使用的应用程序Selenium使用 .NetCore 在 Linux 上使用。这是我的代码实现:
public class Program
{
public static async Task Main(string[] args)
{
//Settings for chrome
var chromeOpts = new ChromeOptions();
chromeOpts.AddArgument("headless");
chromeOpts.AddArgument("no-sandbox");
//Get assembly path where chrome driver is located
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
path = Path.GetDirectoryName(path);
var driver = new ChromeDriver(path, chromeOpts, TimeSpan.FromSeconds(180));
}
}
如您所见,我使用 Chrome 作为驱动程序。我下载了here。我还在程序集的文件夹中添加了驱动程序。通过这种方式,ChromeDriver 已经知道在哪里可以找到它。
在 Linux 上我使用更改了文件夹权限chmod -R 777但是,当我运行 Dotnet 应用程序时,我得到以下信息:
看来Selenium无法启动应用程序。
看看我得到的异常:
找不到 Chrome 二进制文件
不幸的是我在网上没有找到类似的东西。
UPDATE
我重新安装了Chrome
在我的 Linux 机器上,现在上面的错误消失了,但现在还有另一个问题。我收到此错误:
OpenQA.Selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally (Driver info: chromedriver=2.9.248304,platform=Linux 4.4.0-130-generic x86_64)
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(String chromeDriverDirectory, ChromeOptions options, TimeSpan commandTimeout)\ at ODS.Program.Main(String[] args)