Skip to content

Instantly share code, notes, and snippets.

@tqk2811
Created October 18, 2024 12:13
Show Gist options
  • Save tqk2811/b578542f7197bf978765d7eb0924235f to your computer and use it in GitHub Desktop.
Save tqk2811/b578542f7197bf978765d7eb0924235f to your computer and use it in GitHub Desktop.
ChromeDriverService? _service = null;
public int? ChromeProcessId
{
get
{
ProcessHelper processHelper = new ProcessHelper((uint)_service!.ProcessId);
foreach (var child in processHelper.ChildrensProcess)
{
ProcessHelper.Win32_Process? win32_Process = child.Query_Win32_Process();
if (win32_Process?.Name?.Contains("chrome", StringComparison.OrdinalIgnoreCase) == true)
{
return (int)child.ProcessId;
}
}
return null;
}
}
public virtual Task SendKeyOpenFileDialogAsync(
string textSend,
CancellationToken cancellationToken = default
)
=> SendKeyOpenFileDialogAsync(
textSend,
x => x?.Contains("Open", StringComparison.OrdinalIgnoreCase) == true || x?.Contains("Mở", StringComparison.OrdinalIgnoreCase) == true,
cancellationToken
);
public virtual async Task SendKeyOpenFileDialogAsync(
string textSend,
Func<string?, bool> checkTitle,
CancellationToken cancellationToken = default
)
{
int? processId = _profile.ChromeProcessId;
if (!processId.HasValue)
throw new InvalidOperationException($"Không lấy được ChromeProcessId");
ProcessHelper rootProcessHelper = new ProcessHelper((uint)processId.Value);
List<ProcessHelper> processHelpers = [rootProcessHelper, .. rootProcessHelper.ChildrensProcess];
foreach (ProcessHelper processHelper in processHelpers)
{
foreach (WindowHelper windowHelper in processHelper.AllWindows)
{
if (checkTitle.Invoke(windowHelper.Title))
{
WindowHelper? ComboBoxEx32 = windowHelper.ChildrensWindow.FirstOrDefault(x => x.ClassName.Equals("ComboBoxEx32"));
WindowHelper? ComboBox = ComboBoxEx32?.ChildrensWindow?.FirstOrDefault(x => x.ClassName.Equals("ComboBox"));
WindowHelper? Edit = ComboBox?.ChildrensWindow?.FirstOrDefault(x => x.ClassName.Equals("Edit"));
WindowHelper? openButton = windowHelper.ChildrensWindow.FirstOrDefault(x => x.ClassName.Equals("Button") && x.Title.StartsWith("&"));
if (Edit is not null && openButton is not null)
{
await Edit.WindowHandle.SendTextUnicodeAsync(textSend, 10, cancellationToken);
await DelayAsync(100, cancellationToken);
PInvoke.SetActiveWindow(new HWND(windowHelper.WindowHandle));
PInvoke.SetActiveWindow(new HWND(openButton.WindowHandle));
openButton.SendMessage(PInvoke.BM_CLICK, 0, 0);
return;
}
}
}
}
throw new InvalidOperationException("Không tìm được cửa sổ 'OpenFileDialog'");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment