3

I am trying to download a file from a website using a webdriver. But my browser is shutting down before it was able to properly export. I inserted a wait() before I call dispose() but it is not waiting for the time I have specified.

I am doing an implicit wait.

driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, seconds));

Seems to do nothing.
Any ideas??

3
  • Use thread.sleep() method just after you click on download and before your browser closed. you can pass your seconds with sleep method as per your need. Commented May 5, 2015 at 3:36
  • Hello @Lacey Rogers , use driver.wait() method to wait until the file downloads
    – QAMember
    Commented May 5, 2015 at 5:51
  • Are you trying to handle the download popup? Commented May 5, 2015 at 12:38

5 Answers 5

3

Don't use Thread.sleep or ImplicitlyWait. The best way to wait something is explicit wait.

Try to use

new WebDriverWait(driver, 60).until(new ExpectedCondition<Boolean>() {
                @Override
                public Boolean apply(WebDriver webDriver) {
                    return // check if file downloaded
                }

            });
1

Does anything change on the page when the file has loaded? If it does then explicitly wait for that change to occur. I've got a packaged method in my common library that I employ in these situations (packaged so I don't need to write the entire code each time) with a default timeout set to a value that I'd reasonably expect the action to be completed within E.g:

public static IWebElement WaitForElementToAppearOnPage(this WebSiteBase unitTest, IWebElement element, double timeout = 30.00)
        {
            var wait = new WebDriverWait(unitTest.Selenium, TimeSpan.FromSeconds(timeout));
            return wait.Until(driver => element);
        }

I use similar methods to wait for the page to load or for ajax calls to complete.

1

1) driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

This means that we can tell Selenium that we would like it to wait for a certain amount of time before throwing an exception that it cannot find the element on the page. We should note that implicit waits will be in place for the entire time the browser is open. This means that any search for elements on the page could take the time the implicit wait is set for.

2) WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

This wait if maximum time to wait until element is not clickable.

The best way is explicit wait.

Here so many conditions are available e.g. clickable,visible.

3) Thread.sleep(10000);

The last way to slow your execution is use "Thread.sleep(milisecond);".

But is is very hard type of coding.Not recommended for every script

Thank you.

5
  • the question was not about waiting for an element to be clickable.. it was about waiting on a download to complete, which is much less trivial. Commented May 21, 2018 at 22:04
  • @CoreyGoldberg I think questioner need to decide problem. If my answer is in not proper, questioner will mark as wrong.
    – Sagar007
    Commented May 24, 2018 at 10:25
  • @CoreyGoldberg With All due respect, Kindly refer other people's answer. They are typing to give same kind of solution. This solution works for me, It does not mean that it will work for everyone. Kindly put your question. We will try to help you out.
    – Sagar007
    Commented May 28, 2018 at 3:27
  • @Sagat007 you gave a general explanation a waiting technique without answering what the OP asked. The question is about waiting on finished async download before stopping the browser. Commented May 28, 2018 at 12:20
  • @CoreyGoldberg Please put your answer if you are aware of the problem very well. I have resolve this issue with above code and strategy. I am not intelligent like you are. I really like to see your thought process in answer. Kindly give exact or more relative solution.
    – Sagar007
    Commented Jun 5, 2018 at 5:05
0

It doesn't sound as if you need to use Selenium to download the file, just for the browser session. If that is the case, how about instead of downloading the file through the web browser, you pass the browser's session information over to an HTTP library and request the file to be downloaded?

For a specific example, in Python, I would find the URL I am trying to download. Then I would use driver.get_cookies() to ensure I captured the cookies in my current browser session. Finally, I would pass the URL and the cookies to a method which would use requests library to download the file.

An added bonus to this approach: when you need to download multiple files using the same session information, you can gather all the URLs first and download the files from those URLs separately, even splitting the list of URLs between workers fetching the files.

Edit: The question How to download a file using Selenium's WebDriver? adds more information on this topic.

0

anybody minds to share how to convert these 2 lines from C# to VB.NET?

var wait = new WebDriverWait(unitTest.Selenium, TimeSpan.FromSeconds(timeout));
return wait.Until(driver => element);

thank you very much in advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.