0

I have to click on "choose File" button and it opens the fileopen dialog. I need to check if file exists or not. Here is the code

public static void UploadFile(IWebDriver driver, string photoLocation, string photoName)
    {
        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
        IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
        js.ExecuteScript("window.scrollTo(0, document.body.scrollHeight)");
        IWebElement element = driver.FindElement(By.XPath("//*[text() ='Choose File']"));
        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
        js.ExecuteScript("arguments[0].click();", element);           
        // using "HandleOpenDialog" to locate and upload file
        HandleOpenDialog hndOpen = new HandleOpenDialog();
        hndOpen.fileOpenDialog(photoLocation, photoName);
        Thread.Sleep(3000);
    }

This code works properly if the file exists in the location. if it doesn't exists, i want code to look someplace else. The test is stuck there for long time and fails the test.

Can someone help me here?

Madhavi
  • 1
  • 2
  • Please [edit] your question to include a [repro], which should include the error message and stack trace. – Greg Burghardt Jun 26 '23 at 13:10
  • I get the error in a popup: Path does not exist. check the path and try again. I am not sure how to capture that error and try another path. The test stops and fails. i just get the error in the popup. no stacktrace available – Madhavi Jun 27 '23 at 17:16

1 Answers1

0

First of all it looks like HandleOpenDialog is some internal library class that You are using (not able to find it in search engine).

Regarding checking if file exists You can do that by using File.Exists static method (https://learn.microsoft.com/en-us/dotnet/api/system.io.file.exists?view=net-7.0).

sjanisz
  • 57
  • 8