Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selector multiple #721

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class PageActionArgs
/// </summary>
public string[]? IncludeResponseUrls { get; set; }

public List<string>? Selectors { get; set; }

/// <summary>
/// If set to true, the response will be stored in memory
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Microsoft.Playwright;

namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;

public partial class PlaywrightWebDriver
Expand Down Expand Up @@ -47,15 +49,37 @@ await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCall

var response = await page.GotoAsync(args.Url, new PageGotoOptions
{
Timeout = args.Timeout
Timeout = args.Timeout > 0 ? args.Timeout : 30000
});

if (args.Selectors != null)
{
// 使用传入的选择器列表进行并行等待
var tasks =args.Selectors.Select(selector =>
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;
}

await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
if (args.WaitForNetworkIdle)
{
await page.WaitForLoadStateAsync(LoadState.NetworkIdle, new PageWaitForLoadStateOptions
{
Timeout = args.Timeout
Timeout = args.Timeout > 0 ? args.Timeout : 30000
});
}

Expand Down