Click element by position.

This commit is contained in:
Haiping Chen 2024-04-15 23:48:22 -05:00
parent 1814304fe7
commit 70b3e96c0a
7 changed files with 51 additions and 29 deletions

View file

@ -7,12 +7,16 @@ public class ElementActionArgs
private BroswerActionEnum _action;
public BroswerActionEnum Action => _action;
private string _content;
public string Content => _content;
private string? _content;
public string? Content => _content;
public ElementActionArgs(BroswerActionEnum action)
private ElementPosition? _position;
public ElementPosition? Position => _position;
public ElementActionArgs(BroswerActionEnum action, ElementPosition? position = null)
{
_action = action;
_position = position;
}
public ElementActionArgs(BroswerActionEnum action, string content)

View file

@ -0,0 +1,8 @@
namespace BotSharp.Abstraction.Browsing.Models;
public class ElementPosition
{
public float X { get; set; } = default!;
public float Y { get; set; } = default!;
}

View file

@ -133,8 +133,8 @@
<ItemGroup>
<PackageReference Include="Aspects.Cache" Version="2.0.4" />
<PackageReference Include="Colorful.Console" Version="1.2.15" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="6.3.1" />
<PackageReference Include="Fluid.Core" Version="2.7.0" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.1.1"/>
<PackageReference Include="Fluid.Core" Version="2.8.0" />
<PackageReference Include="Nanoid" Version="3.0.0" />
</ItemGroup>

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
@ -33,7 +33,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.28" />
<PackageReference Include="MySqlConnector" Version="2.3.5" />
</ItemGroup>

View file

@ -40,13 +40,13 @@ public class PlaywrightInstance : IDisposable
Channel = "chrome",
IgnoreDefaultArgs = new[]
{
"--disable-infobars"
},
"--disable-infobars"
},
Args = new[]
{
"--disable-infobars",
// "--start-maximized"
}
"--disable-infobars",
// "--start-maximized"
}
});
_contexts[id].Page += async (sender, e) =>

View file

@ -9,7 +9,21 @@ public partial class PlaywrightWebDriver
if (action.Action == BroswerActionEnum.Click)
{
await locator.ClickAsync();
if (action.Position == null)
{
await locator.ClickAsync();
}
else
{
await locator.ClickAsync(new LocatorClickOptions
{
Position = new Position
{
X = action.Position.X,
Y = action.Position.Y
}
});
}
}
else if (action.Action == BroswerActionEnum.InputText)
{

View file

@ -23,24 +23,21 @@ public partial class PlaywrightWebDriver
}
var page = await _instance.NewPage(contextId);
if (!string.IsNullOrEmpty(url))
try
{
try
var response = await page.GotoAsync(url, new PageGotoOptions
{
var response = await page.GotoAsync(url, new PageGotoOptions
{
Timeout = 15 * 1000
});
await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
result.IsSuccess = response.Status == 200;
}
catch(Exception ex)
{
result.Message = ex.Message;
result.StackTrace = ex.StackTrace;
_logger.LogError(ex.Message);
}
Timeout = 15 * 1000
});
await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
result.IsSuccess = response.Status == 200;
}
catch (Exception ex)
{
result.Message = ex.Message;
result.StackTrace = ex.StackTrace;
_logger.LogError(ex.Message);
}
}