BotSharp/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs

77 lines
2.4 KiB
C#
Raw Normal View History

2024-06-21 18:23:18 +00:00
using System.Linq;
2024-02-01 03:29:13 +00:00
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
public partial class PlaywrightWebDriver
{
2024-06-10 15:51:14 +00:00
public async Task<BrowserActionResult> GoToPage(MessageInfo message, PageActionArgs args)
2024-02-01 03:29:13 +00:00
{
2024-03-08 13:17:36 +00:00
var result = new BrowserActionResult();
2024-05-31 19:26:27 +00:00
var context = await _instance.InitInstance(message.ContextId);
2024-02-10 18:14:18 +00:00
try
{
// Check if the page is already open
2024-06-21 18:23:18 +00:00
/*if (!args.OpenNewTab && context.Pages.Count > 0)
{
2024-05-31 19:26:27 +00:00
foreach (var p in context.Pages)
{
2024-06-10 15:51:14 +00:00
if (p.Url == args.Url)
2024-05-31 19:26:27 +00:00
{
2024-06-10 15:51:14 +00:00
// Disable this due to performance issue, some page is too large
// result.Body = await p.ContentAsync();
2024-05-31 19:26:27 +00:00
result.IsSuccess = true;
2024-06-15 04:38:38 +00:00
// await p.BringToFrontAsync();
2024-05-31 19:26:27 +00:00
return result;
}
}
2024-06-21 18:23:18 +00:00
}*/
2024-06-21 18:23:18 +00:00
var page = args.OpenNewTab ? await _instance.NewPage(message.ContextId, fetched: args.OnDataFetched) :
2024-05-31 19:26:27 +00:00
_instance.GetPage(message.ContextId);
2024-06-21 18:23:18 +00:00
2024-06-26 12:48:15 +00:00
var response = await page.GotoAsync(args.Url, new PageGotoOptions
{
Timeout = args.Timeout
});
2024-04-09 03:38:56 +00:00
await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
2024-06-26 12:48:15 +00:00
if (args.WaitForNetworkIdle)
{
await page.WaitForLoadStateAsync(LoadState.NetworkIdle, new PageWaitForLoadStateOptions
{
Timeout = args.Timeout
});
}
2024-02-10 18:14:18 +00:00
2024-03-08 13:17:36 +00:00
if (response.Status == 200)
{
2024-05-31 03:24:34 +00:00
// Disable this due to performance issue, some page is too large
// result.Body = await page.InnerHTMLAsync("body");
2024-03-08 13:17:36 +00:00
result.IsSuccess = true;
}
else
{
2024-04-08 15:09:08 +00:00
result.Message = response.StatusText;
2024-03-08 13:17:36 +00:00
}
2024-02-10 18:14:18 +00:00
}
catch (Exception ex)
{
2024-04-08 15:09:08 +00:00
result.Message = ex.Message;
2024-03-08 13:17:36 +00:00
result.StackTrace = ex.StackTrace;
2024-02-10 18:14:18 +00:00
_logger.LogError(ex.Message);
}
2024-03-08 13:17:36 +00:00
return result;
2024-02-01 03:29:13 +00:00
}
2024-06-21 18:23:18 +00:00
private void Page_Response1(object sender, IResponse e)
{
throw new NotImplementedException();
}
private void Page_Response(object sender, IResponse e)
{
throw new NotImplementedException();
}
2024-02-01 03:29:13 +00:00
}