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-06-30 13:05:57 +00:00
|
|
|
var context = await _instance.GetContext(message.ContextId);
|
2024-02-10 18:14:18 +00:00
|
|
|
try
|
|
|
|
|
{
|
2024-11-20 01:06:42 +00:00
|
|
|
var page = await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCallback,
|
2024-08-14 03:44:53 +00:00
|
|
|
responseInMemory: args.ResponseInMemory,
|
|
|
|
|
responseContainer: args.ResponseContainer,
|
2024-08-12 14:43:39 +00:00
|
|
|
excludeResponseUrls: args.ExcludeResponseUrls,
|
|
|
|
|
includeResponseUrls: args.IncludeResponseUrls);
|
2024-08-04 22:48:52 +00:00
|
|
|
|
2024-07-09 12:15:03 +00:00
|
|
|
Serilog.Log.Information($"goto page: {args.Url}");
|
2024-08-04 22:48:52 +00:00
|
|
|
|
2024-11-07 05:21:51 +00:00
|
|
|
if (args.OpenNewTab && page != null && page.Url == "about:blank")
|
2024-08-05 00:15:27 +00:00
|
|
|
{
|
2024-11-20 01:06:42 +00:00
|
|
|
page = await _instance.NewPage(message,
|
2024-08-14 03:44:53 +00:00
|
|
|
enableResponseCallback: args.EnableResponseCallback,
|
|
|
|
|
responseInMemory: args.ResponseInMemory,
|
|
|
|
|
responseContainer: args.ResponseContainer,
|
2024-08-12 14:43:39 +00:00
|
|
|
excludeResponseUrls: args.ExcludeResponseUrls,
|
|
|
|
|
includeResponseUrls: args.IncludeResponseUrls);
|
2024-08-05 00:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-13 02:58:06 +00:00
|
|
|
if (page == null)
|
|
|
|
|
{
|
2024-11-20 01:06:42 +00:00
|
|
|
page = await _instance.NewPage(message,
|
2024-08-14 03:44:53 +00:00
|
|
|
enableResponseCallback: args.EnableResponseCallback,
|
|
|
|
|
responseInMemory: args.ResponseInMemory,
|
|
|
|
|
responseContainer: args.ResponseContainer,
|
2024-08-13 02:58:06 +00:00
|
|
|
excludeResponseUrls: args.ExcludeResponseUrls,
|
|
|
|
|
includeResponseUrls: args.IncludeResponseUrls);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-18 14:15:23 +00:00
|
|
|
// Active current tab
|
|
|
|
|
await page.BringToFrontAsync();
|
|
|
|
|
|
2024-06-26 12:48:15 +00:00
|
|
|
var response = await page.GotoAsync(args.Url, new PageGotoOptions
|
|
|
|
|
{
|
2024-11-03 16:41:01 +00:00
|
|
|
Timeout = args.Timeout > 0 ? args.Timeout : 30000
|
2024-06-26 12:48:15 +00:00
|
|
|
});
|
|
|
|
|
|
2024-11-03 16:41:01 +00:00
|
|
|
if (args.Selectors != null)
|
|
|
|
|
{
|
|
|
|
|
// 使用传入的选择器列表进行并行等待
|
2024-11-20 01:06:42 +00:00
|
|
|
var tasks = args.Selectors.Select(selector =>
|
2024-11-03 16:41:01 +00:00
|
|
|
page.WaitForSelectorAsync(selector, new PageWaitForSelectorOptions
|
|
|
|
|
{
|
|
|
|
|
Timeout = args.Timeout > 0 ? args.Timeout : 30000
|
|
|
|
|
})
|
|
|
|
|
).ToArray();
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(tasks);
|
|
|
|
|
|
|
|
|
|
// 在此处提取所有选择器的 HTML 内容
|
|
|
|
|
var contentTasks = args.Selectors.Select(selector => page.InnerHTMLAsync(selector)).ToArray();
|
|
|
|
|
var contents = await Task.WhenAll(contentTasks);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess = true;
|
|
|
|
|
result.Body = string.Join(", ", contents.Select((content, index) => $"{args.Selectors[index]}: {content}"));
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2024-11-03 16:41:01 +00:00
|
|
|
Timeout = args.Timeout > 0 ? args.Timeout : 30000
|
2024-06-26 12:48:15 +00:00
|
|
|
});
|
|
|
|
|
}
|
2024-02-10 18:14:18 +00:00
|
|
|
|
2024-08-04 22:48:52 +00:00
|
|
|
if (args.WaitTime > 0)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(args.WaitTime * 1000);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 12:21:35 +00:00
|
|
|
result.ResponseStatusCode = response.Status;
|
2024-03-08 13:17:36 +00:00
|
|
|
if (response.Status == 200)
|
|
|
|
|
{
|
|
|
|
|
result.IsSuccess = true;
|
2024-10-08 01:20:48 +00:00
|
|
|
|
|
|
|
|
// Be careful if page is too large, it will cause performance issue
|
|
|
|
|
if (args.ReadInnerHTMLAsBody)
|
|
|
|
|
{
|
|
|
|
|
result.Body = await page.InnerHTMLAsync("body");
|
|
|
|
|
}
|
2024-03-08 13:17:36 +00:00
|
|
|
}
|
|
|
|
|
else
|
2024-11-20 01:06:42 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
}
|