gpt4 book ai didi

c# - 检测浏览器中是否弹出打开文件对话框

转载 作者:行者123 更新时间:2023-12-05 07:17:31 24 4
gpt4 key购买 nike

我正在使用 C# 和 selenium(运行 Google Chrome)自动上传文件,但我不想在文件输入中键入文件路径,我想模拟真实用户的操作方式(键入文件对话框中的路径)。使用 SendKeys.SendWait() 方法是完美的解决方案,但我遇到了问题。

我有以下代码。

var e = driverWait.Until(driver => driver.FindElement(By.Id("file-button")));
e.Click();
SendKeys.SendWait(@"C:\Users\seawolf\Downloads\Result.pdf");

问题是在打开文件对话框弹出之前,SendWait 开始在键盘上输入,所以路径是在网页而不是对话框中输入的。

有没有办法用 C# 弹出一个打开的文件对话框?它不必内置到 selenium 中,因为它很容易将所有内容与 driverWait 集成。我想做的是这样的:

driverWait.Until(_ => DialogIsOpen() );

感谢@Jimi,这是我的解决方案。

var chromeElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "Chrome_WidgetWin_1"));
var openDialogClassName = "#32770";
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, chromeElement, TreeScope.Subtree, (sender, _) =>
{
var element = sender as AutomationElement;
Console.WriteLine("running");
if (element.Current.ClassName.Equals(openDialogClassName))
{
SendKeys.SendWait(@"C:\Users\seawolf\Downloads\Result.pdf");
}
});

最佳答案

这是我用的。在使用 sendwait 之前,我只是等待窗口出现。大多数情况下,您会在上传窗口出现后将焦点切换到它。

//Click browse btn to open upload window
driver.FindElement(By.ClassName("uploadbtn")).Click();

//Wait for Upload Window to appear(may not be necessary in some cases)
Thread.Sleep(2000);

//Send the path and then click the Right arrow key
SendKeys.SendWait(GetProjectRoot() + @"\DataFiles\red.png" + @"{RIGHT}");

//Click Enter Key to submit
SendKeys.SendWait(@"{Enter}");

关于c# - 检测浏览器中是否弹出打开文件对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58790888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com