0

I am running the script where once clicked, Download button which gives options to download 5 different types of file. Please see the attached image [https://i.stack.imgur.com/ra8Cn.png], [https://i.stack.imgur.com/fHvC9.png].

5 times the download button needs to be clicked, one after other, to download the files.

It's working fine, but sometimes it's just Download the "Image" file and text execution ends with passing result without downloading further options.

I tried to click on Download button with the help of mainDownloadbutton.click(), act.movetoElement(mainDownloadbutton).click().build().perform(), using JavaScript and at last tried with Sikuli to click. But having same issue.

Here is code:

public void downloadOptions() throws InterruptedException, FindFailed {
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='Data Visualization']")));
                    
        try {
//Image Option
            //wait.until(ExpectedConditions.elementToBeClickable(mainDownloadButton));
            mainDownloadButton.click();
            log.info("Selected the opiton 'Download'. ");   //  Thread.sleep(2000);
            selectImageOption.click();
            log.info("Selected the opiton 'Image' to be downloaded.");  
            //wait.until(ExpectedConditions.elementToBeClickable(clickDownloadOption));
            //clickDownloadOption.click();
            log.info("PNG File Downloaded");
                            
        // Crosstab Option
            Thread.sleep(2000);
            //mainDownloadButton.click();
            Pattern image4 = new Pattern("C:\\Selenium\\workspace\\HealthCare_ABCTest\\Screenshots\\Download.PNG");
            screen.wait(image4, 10);
            screen.mouseMove(image4);
            screen.click(image4);
                
            wait.until(ExpectedConditions.elementToBeClickable(selectCrosstabOption));
            selectCrosstabOption.click();
            selectCrosstabDownloadOption.click();
            Pattern image5 = new Pattern("C:\\Selenium\\workspace\\HealthCare_ABCTest\\Screenshots\\Allow.PNG");
            screen.wait(image5, 10);
            screen.mouseMove(image5);
            screen.click(image5);
            log.info("PNG File Downloaded");
            
        //PDF options
            Thread.sleep(2000);
            //wait.until(ExpectedConditions.elementToBeClickable(mainDownloadButton));
            //mainDownloadButton.click();
            screen.wait(image4, 10);
            screen.mouseMove(image4);
            screen.click(image4);
            log.info("Selected the opiton 'Download'. ");// Thread.sleep(2000);
            //wait.until(ExpectedConditions.elementToBeClickable(selectPDFOption));
            selectPDFOption.click();
            log.info("Selected the opiton 'PDF' to be downloaded");
            act.moveToElement(selectPDFDownloadOption).click().build().perform();
            log.info("PDF File Downloaded");
                        
        //PowerPoint Option  
            Thread.sleep(2000);
            screen.wait(image4, 10);
            screen.mouseMove(image4);
            screen.click(image4);
            log.info("Selected the opiton 'Download'. ");//wait.until(ExpectedConditions.elementToBeClickable(selectPowerPointOption));
            selectPowerPointOption.click();
            //wait.until(ExpectedConditions.elementToBeClickable(selectPPDownloadOption));
            selectPPDownloadOption.click();
            log.info("PPT Downloaded");
            
        //Data Option
            //wait.until(ExpectedConditions.elementToBeClickable(mainDownloadButton));
             mainDownloadButton.click();
            Thread.sleep(2000);
            screen.wait(image4, 10);
            screen.mouseMove(image4);
            screen.click(image4);
            log.info("Selected the opiton 'Download'. ");// Thread.sleep(2300);
            String parentHandle = driver.getWindowHandle(); // get the current window handle
            wait.until(ExpectedConditions.elementToBeClickable(selectDataOption));
            selectDataOption.click(); // click some link that opens a new window
            log.info("Selected the opiton 'Data' to be downloaded.");
            for (String winHandle : driver.getWindowHandles()) {
            driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
                    }
            wait.until(ExpectedConditions.elementToBeClickable(downloadAllRowsAsATextFile));
    
            downloadAllRowsAsATextFile.click();
            Thread.sleep(500);
    
            driver.close(); // close newly opened window when done with it
            driver.switchTo().window(parentHandle);
            log.info("Data File Downloaded");
            
            
        }catch (org.openqa.selenium.StaleElementReferenceException ex) {
            
            wait.until(ExpectedConditions.elementToBeClickable(mainDownloadButton));
        }
        catch(ElementClickInterceptedException e)
        {
            
            wait.until(ExpectedConditions.elementToBeClickable(mainDownloadButton));
        }

        
        driver.switchTo().defaultContent();
    }
1
  • Why are you trying to download multiple files in one method? Can you create 1 downloadOptions(downloadButtonElement) method with a parameter that changes which download button to press? This will make your code more reusable, readable. Might want to review POM design pattern. – Lee Jensen May 11 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.