1

I have a selenium test that works great while debugging and stepping slowly through. I added a Thread.Sleep() in order to stop selenium while a file is downloaded. However selenium in completely ignoring the Thread.Sleep(). Any thoughts?

IWebElement iframe = driver.FindElement(By.Id("genFrame"));
CurrentFrame = driver.SwtichTo();
CurrentFrame.Frame(iframe);
driver.FindElement(By.Id("date").Sendkeys("05/11/2021" + Keys.Enter));
driver.FindElement(By.Id("export-btn"));
System.Thread.Sleep(5000);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Bean0341
  • 1,632
  • 2
  • 17
  • 37
  • Using `Thread.Sleep` is just trying to guess if download has finished. You need to use some other approach, for example - monitoring [download folder](https://stackoverflow.com/questions/28586602/wait-for-finished-download-file-in-selenium-and-c-sharp). – Guru Stron Apr 05 '22 at 19:33
  • @GuruStron your link just take me to another SO solution using thread.sleep(). – Bean0341 Apr 05 '22 at 19:59
  • It uses some code besides `Thread.Sleep`. – Guru Stron Apr 05 '22 at 20:00

1 Answers1

1

In all possibilities System.Thread.Sleep() is never ignored unless some previous line of code raises an exception.


As per your code the unit is milliseconds, so effectively 5000 milliseconds, i.e. 5 seconds.


Solution

You may like to increase the waiting time to greater value, e.g. 10 seconds or more:

using System;  
using System.Threading;

System.Thread.Sleep(10000);

Update

As you can visually see selenium add the date field, possibly the line in the mid:

driver.FindElement(By.Id("export-btn"));

raises an exception.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I have increased the time to a silly number to ensure the sleep is being seen. I visually see selenium add the date field, then immediately close the driver. I assure you the sleep is completely being ignored for some reason. – Bean0341 Apr 05 '22 at 19:44
  • @Bean0341 _immediately close the driver_: Possibly the line in the mid `driver.FindElement(By.Id("export-btn"));` raises an exception? – undetected Selenium Apr 05 '22 at 20:13