1

I have searched StackOverflow and could not find the correct solution for the following problem:

In an internal Sharepoint site, after clicking Upload, and then Folders, I use SendKeys.SendWait to input the directory location,. And then after sending "Enter" as Send.Keys.SendWait, a dialogue box appears, where I need to click the Upload button, but this element is not searchable, as it seems to be not part of the DOM. enter image description here

Here is the code snippet:

SendKeys.SendWait(@"C:\Automation\Testing\");

SendKeys.SendWait("{Enter}");

SendKeys.SendWait("{Enter}");

IAlert alert = Driver.driver.SwitchTo().Alert();
alert.Accept();

I have also tried to use JavaScript to handle this but it didn't help:

IJavaScriptExecutor jsX = (IJavaScriptExecutor)driver;

jsX.ExecuteScript("Object.defineProperty(BeforeUnloadEvent.prototype, 
'returnValue', { get:function(){}, set:function(){} });");

In another thread I noticed a suggestion to add "--disable-notifications" and "--disable-popup-blocking", but that didn't work either.

Here is the code:

ChromeOptions options = new ChromeOptions();
options.AddArguments("disable-infobars", "start-maximized", "no-sandbox", "--disable-notifications", "--disable-popup-blocking");
 Driver.driver = new ChromeDriver(options);

What is the best way to find and click the "Upload" in the dialogue box?

Sohel
  • 656
  • 2
  • 11
  • 31
  • check these answers: https://stackoverflow.com/questions/40021047/selenium-webdriver-upload-document-to-non-input-button/40021733#40021733 and https://stackoverflow.com/questions/49540188/upload-image-through-macos-window-with-selenium/49540578#49540578 – Florent B. Sep 17 '19 at 08:41

1 Answers1

1

You may try to use InputSimulator Nuget for simulating keyboard buttons events. On the screenshot Cancel button is selected, pressing Tab key might change the focus to Upload button, then Enter key may press Upload button:

var sim = new WindowsInput.InputSimulator();
System.Threading.Thread.Sleep(3000);
sim.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.TAB);
System.Threading.Thread.Sleep(300);
sim.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.RETURN);
Renat
  • 7,718
  • 2
  • 20
  • 34
  • Hi Renat, Thank you for your feedback. I have just tried InputSimulator, it could not simulate the Upload click. It just simulated Enter key action that got the popup window disappeared without uploading any files. – Sohel Sep 16 '19 at 22:36
  • 1
    @Sohel. Ok, I've update my answer. At first I got a wrong impression that `Upload` was selected, but on the screenshot actually `Cancel` button is selected, so Tab -> Enter might still work. – Renat Sep 16 '19 at 23:25
  • 1
    Thank you Renat, your last modified code made it work. Thank you. – Sohel Sep 17 '19 at 16:56
  • Glad it was helpful – Renat Sep 17 '19 at 16:59