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

47 lines
1.5 KiB
C#
Raw Normal View History

2024-02-15 20:37:42 +00:00
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
public partial class PlaywrightWebDriver
{
2024-06-10 15:51:14 +00:00
public async Task<BrowserActionResult> ScrollPage(MessageInfo message, PageActionArgs args)
2024-02-15 20:37:42 +00:00
{
2024-03-08 13:17:36 +00:00
var result = new BrowserActionResult();
2024-06-10 15:51:14 +00:00
await _instance.Wait(message.ContextId);
2024-02-15 20:37:42 +00:00
2024-06-10 15:51:14 +00:00
var page = _instance.GetPage(message.ContextId);
2024-02-15 20:37:42 +00:00
2024-06-10 15:51:14 +00:00
if (args.Direction == "down")
{
// Get the total page height
int scrollY = await page.EvaluateAsync<int>("document.body.scrollHeight");
// Scroll to the bottom
await page.Mouse.WheelAsync(0, scrollY);
}
else if (args.Direction == "up")
{
// Get the total page height
int scrollY = await page.EvaluateAsync<int>("document.body.scrollHeight");
// Scroll to the bottom
await page.Mouse.WheelAsync(0, -scrollY);
}
else if (args.Direction == "left")
{
await page.EvaluateAsync(@"
var scrollingElement = (document.scrollingElement || document.body);
scrollingElement.scrollLeft = 0;
");
}
else if (args.Direction == "right")
{
await page.EvaluateAsync(@"
var scrollingElement = (document.scrollingElement || document.body);
scrollingElement.scrollLeft = scrollingElement.scrollWidth;
");
}
2024-02-15 20:37:42 +00:00
2024-03-08 13:17:36 +00:00
result.IsSuccess = true;
return result;
2024-02-15 20:37:42 +00:00
}
}