I am automating a file upload with C# and selenium (running Google Chrome), but I don't want to type the file path on the file input, I would like to emulate how a real user would do it (Typing the path in the file dialog box). Using the SendKeys.SendWait() method is the perfect solution for this, however I am encountering a problem.
I have the following code.
var e = driverWait.Until(driver => driver.FindElement(By.Id("file-button")));
e.Click();
SendKeys.SendWait(@"C:\Users\seawolf\Downloads\Result.pdf");
The problem is before the open file dialog pops up, SendWait begins typing on the keyboard, so the path is typed on the Webpage and not the dialog box.
Is there a way until an open file dialog pops up with C#? It doesn't have to be built into selenium as it's easy to integrate everything with driverWait. What I want to do is something like:
driverWait.Until(_ => DialogIsOpen() );
Thanks to @Jimi, here is my solution.
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");
}
});