Remove Selenium
This commit is contained in:
parent
74c797abf2
commit
f1e8ceefca
|
|
@ -4,6 +4,7 @@ namespace BotSharp.Abstraction.Browsing;
|
|||
|
||||
public interface IWebBrowser
|
||||
{
|
||||
void SetServiceProvider(IServiceProvider services);
|
||||
Task<BrowserActionResult> LaunchBrowser(MessageInfo message, BrowserActionArgs args);
|
||||
Task<BrowserActionResult> ScreenshotAsync(MessageInfo message, string path);
|
||||
Task<BrowserActionResult> ScrollPage(MessageInfo message, PageActionArgs args);
|
||||
|
|
|
|||
|
|
@ -14,4 +14,9 @@ public class PageActionArgs
|
|||
|
||||
public bool WaitForNetworkIdle { get; set; } = true;
|
||||
public float? Timeout { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Wait time in seconds after page is opened
|
||||
/// </summary>
|
||||
public int WaitTime { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -170,7 +170,7 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Aspects.Cache" Version="2.0.4" />
|
||||
<PackageReference Include="DistributedLock.Redis" Version="1.0.3" />
|
||||
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.5.0" />
|
||||
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.5.1" />
|
||||
<PackageReference Include="Fluid.Core" Version="2.11.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
|
||||
|
|
|
|||
|
|
@ -11,15 +11,17 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Drivers\SeleniumDriver\**" />
|
||||
<Compile Remove="packages\**" />
|
||||
<EmbeddedResource Remove="Drivers\SeleniumDriver\**" />
|
||||
<EmbeddedResource Remove="packages\**" />
|
||||
<None Remove="Drivers\SeleniumDriver\**" />
|
||||
<None Remove="packages\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Playwright" Version="1.45.1" />
|
||||
<PackageReference Include="Selenium.WebDriver" Version="4.23.0" />
|
||||
<PackageReference Include="HtmlAgilityPack" Version="1.11.61" />
|
||||
<PackageReference Include="HtmlAgilityPack" Version="1.11.62" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -18,9 +18,22 @@ public class PlaywrightInstance : IDisposable
|
|||
/// </summary>
|
||||
public Dictionary<string, List<IPage>> Pages => _pages;
|
||||
|
||||
public IPage GetPage(string id, string? pattern = null)
|
||||
public IPage GetPage(string contextId, string? pattern = null)
|
||||
{
|
||||
return _contexts[id].Pages.LastOrDefault();
|
||||
if (string.IsNullOrEmpty(pattern))
|
||||
{
|
||||
return _contexts[contextId].Pages.LastOrDefault();
|
||||
}
|
||||
|
||||
foreach (var page in _contexts[contextId].Pages)
|
||||
{
|
||||
if (page.Url.ToLower() == pattern.ToLower())
|
||||
{
|
||||
return page;
|
||||
}
|
||||
}
|
||||
|
||||
return _contexts[contextId].Pages.LastOrDefault();
|
||||
}
|
||||
|
||||
public async Task<IBrowserContext> GetContext(string ctxId)
|
||||
|
|
@ -103,38 +116,6 @@ public class PlaywrightInstance : IDisposable
|
|||
var js = @"Object.defineProperties(navigator, {webdriver:{get:()=>false}});";
|
||||
await page.AddInitScriptAsync(js);
|
||||
|
||||
page.Response += async (sender, e) =>
|
||||
{
|
||||
if (e.Headers.ContainsKey("content-type") &&
|
||||
e.Headers["content-type"].Contains("application/json") &&
|
||||
(e.Request.ResourceType == "fetch" || e.Request.ResourceType == "xhr"))
|
||||
{
|
||||
Serilog.Log.Information($"{e.Request.Method}: {e.Url}");
|
||||
JsonElement? json = null;
|
||||
try
|
||||
{
|
||||
if (e.Status == 200 && e.Ok)
|
||||
{
|
||||
json = await e.JsonAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serilog.Log.Warning($"Response status: {e.Status} {e.StatusText}, OK: {e.Ok}");
|
||||
}
|
||||
|
||||
var webPageResponseHooks = services.GetServices<IWebPageResponseHook>();
|
||||
foreach (var hook in webPageResponseHooks)
|
||||
{
|
||||
hook.OnDataFetched(message, e.Url.ToLower(), e.Request?.PostData ?? string.Empty, JsonSerializer.Serialize(json));
|
||||
}
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Serilog.Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,9 +25,49 @@ public partial class PlaywrightWebDriver
|
|||
}*/
|
||||
|
||||
var page = args.OpenNewTab ? await _instance.NewPage(message, _services) :
|
||||
_instance.GetPage(message.ContextId);
|
||||
_instance.GetPage(message.ContextId, pattern: args.Url);
|
||||
|
||||
page.Response += async (sender, e) =>
|
||||
{
|
||||
if (e.Headers.ContainsKey("content-type") &&
|
||||
e.Headers["content-type"].Contains("application/json") &&
|
||||
(e.Request.ResourceType == "fetch" || e.Request.ResourceType == "xhr"))
|
||||
{
|
||||
Serilog.Log.Information($"{e.Request.Method}: {e.Url}");
|
||||
JsonElement? json = null;
|
||||
try
|
||||
{
|
||||
if (e.Status == 200 && e.Ok)
|
||||
{
|
||||
json = await e.JsonAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serilog.Log.Warning($"Response status: {e.Status} {e.StatusText}, OK: {e.Ok}");
|
||||
}
|
||||
|
||||
var webPageResponseHooks = _services.GetServices<IWebPageResponseHook>();
|
||||
foreach (var hook in webPageResponseHooks)
|
||||
{
|
||||
hook.OnDataFetched(message, e.Url.ToLower(), e.Request?.PostData ?? string.Empty, JsonSerializer.Serialize(json));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Serilog.Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (!args.OpenNewTab && page != null && page.Url != "about:blank")
|
||||
{
|
||||
Serilog.Log.Information($"goto existing page: {args.Url}");
|
||||
result.IsSuccess = true;
|
||||
return result;
|
||||
}
|
||||
|
||||
Serilog.Log.Information($"goto page: {args.Url}");
|
||||
|
||||
var response = await page.GotoAsync(args.Url, new PageGotoOptions
|
||||
{
|
||||
Timeout = args.Timeout
|
||||
|
|
@ -42,6 +82,11 @@ public partial class PlaywrightWebDriver
|
|||
});
|
||||
}
|
||||
|
||||
if (args.WaitTime > 0)
|
||||
{
|
||||
await Task.Delay(args.WaitTime * 1000);
|
||||
}
|
||||
|
||||
if (response.Status == 200)
|
||||
{
|
||||
// Disable this due to performance issue, some page is too large
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
|
|||
|
||||
public partial class PlaywrightWebDriver : IWebBrowser
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private IServiceProvider _services;
|
||||
private readonly PlaywrightInstance _instance;
|
||||
private readonly ILogger _logger;
|
||||
public PlaywrightInstance Instance => _instance;
|
||||
|
|
@ -63,4 +63,9 @@ public partial class PlaywrightWebDriver : IWebBrowser
|
|||
|
||||
return element;
|
||||
}
|
||||
|
||||
public void SetServiceProvider(IServiceProvider services)
|
||||
{
|
||||
_services = services;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -81,4 +81,9 @@ public partial class SeleniumWebDriver : IWebBrowser
|
|||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void SetServiceProvider(IServiceProvider services)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ global using Microsoft.Playwright;
|
|||
global using Microsoft.Extensions.Configuration;
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
global using Microsoft.Extensions.Logging;
|
||||
global using OpenQA.Selenium;
|
||||
global using BotSharp.Abstraction.Browsing.Enums;
|
||||
global using BotSharp.Abstraction.Conversations;
|
||||
global using BotSharp.Abstraction.Plugins;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
using BotSharp.Abstraction.Browsing.Settings;
|
||||
using BotSharp.Abstraction.Settings;
|
||||
using BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
|
||||
using BotSharp.Plugin.WebDriver.Drivers.SeleniumDriver;
|
||||
using BotSharp.Plugin.WebDriver.Hooks;
|
||||
|
||||
namespace BotSharp.Plugin.Playwrights;
|
||||
|
|
@ -12,7 +11,7 @@ public class WebDriverPlugin : IBotSharpPlugin
|
|||
public string Name => "Web Driver";
|
||||
public string Description => "Empower agent to manipulate web browser in automation tools.";
|
||||
public string IconUrl => "https://cdn-icons-png.flaticon.com/512/8576/8576378.png";
|
||||
public string[] AgentIds => new[] { "f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b" };
|
||||
public string[] AgentIds => ["f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b"];
|
||||
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
|
|
@ -28,13 +27,13 @@ public class WebDriverPlugin : IBotSharpPlugin
|
|||
services.AddScoped<PlaywrightWebDriver>();
|
||||
services.AddSingleton<PlaywrightInstance>();
|
||||
|
||||
services.AddScoped<SeleniumWebDriver>();
|
||||
services.AddSingleton<SeleniumInstance>();
|
||||
// services.AddScoped<SeleniumWebDriver>();
|
||||
// services.AddSingleton<SeleniumInstance>();
|
||||
|
||||
services.AddScoped<IWebBrowser>(provider => settings.Driver switch
|
||||
{
|
||||
"Playwright" => provider.GetRequiredService<PlaywrightWebDriver>(),
|
||||
"Selenium" => provider.GetRequiredService<SeleniumWebDriver>(),
|
||||
// "Selenium" => provider.GetRequiredService<SeleniumWebDriver>(),
|
||||
_ => provider.GetRequiredService<PlaywrightWebDriver>(),
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue