How can you effectively manage a Windows pop-up for downloading files and save them using Selenium and Java?

public static FirefoxProfile customFirefoxDriverProfile() throws Exception {
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("browser.download.folderList", 2);
    profile.setPreference("browser.download.manager.showWhenStarting",false);
    profile.setPreference("browser.download.dir",Constant.downloadPath);
    profile.setPreference("browser.helperApps.neverAsk.openFile",
               "text/csv;application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
               "text/csv;application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    profile.setPreference("browser.helperApps.alwaysAsk.force", false);
    profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
    profile.setPreference("browser.download.manager.focusWhenStarting", false);
    profile.setPreference("browser.download.manager.useWindow", false);
    profile.setPreference("browser.download.manager.showAlertOnComplete",false);
    profile.setPreference("browser.download.manager.closeWhenDone", false);
    profile.setPreference("pdfjs.disabled", true);

    return profile;
}

@Test
public static synchronized WebDriver createDriverInstance(String browser) throws Exception{
    // If the browser is Firefox, then do this
    if (Constant.BROWSER_FIREFOX.equalsIgnoreCase(browser)) {

        fd = new FirefoxDriver(customFirefoxDriverProfile());
        fd.manage().window().maximize();
        fd.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

    }
}

The code above aims to automatically download files without displaying windows pop-ups, but it seems to be malfunctioning. The references used in this code were gathered from various websites and stackoverflow. As a beginner in selenium testing, I am still learning.

Answer №1

If the code provided is all you have, it may not be enough to initiate a download. Consider using Robot() instead of forcibly disabling windows. Below is an example of how you can modify your code:

public static void copyPaste (String content) throws AWTException {
    // Store the path (passed as 'content' String) to the saving folder in RAM
    StringSelection selection = new StringSelection(content);
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(selection, selection);

    // Initialize Robot()
    Robot robot = new Robot();

    // Wait for some time for the save dialog to open
    robot.delay(3000);

    // Imitate user's actions - paste what was stored in RAM previously
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);

    // Submit and the download should start
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
}

This method can be used after opening the download window dialog.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to include parameters in JUnit tests similar to how it is done in TestNG? If so, how can this

Is it possible to incorporate parameters into JUnit tests in a similar way as done in TestNG with xml files? I am aware of parameterized tests in JUnit, but that is not what I am looking for. Currently, I handle it like this: String example = "test"; Str ...

The objects fail to initialize when using PageFactory.initElements(driver,class);

I am facing an issue while trying to initialize objects of the Homepage class Everything was working fine until yesterday, but for some unknown reason today it's not functioning properly package StepDefnitions; import org.openqa.selenium.WebDri ...

Decomposing the Retrofit response.body()

I am having difficulties with implementing Retrofit and understanding how to interpret the response body. I believe that there might be an issue with mapping my JSON data to POJO because the output is not what I expected when I print it out in the log. He ...

Converting JSON to XML while preserving the original order of elements

Utilizing the following code snippet to translate JSON into XML across multiple XML files with varying JSON structures. String toXmlRequest = fullRequest.toString(); JSONObject jsonObj = new JSONObject(toXmlRequest); String X ...

What is the best approach to interact with and click on a link nested within a button using Selenium

Looking for some assistance. How can I successfully click on a button that is nested inside a div? Currently, I am unable to achieve this action with my current code. Below is the snippet of my code: > WebElement btn_Submit = > driver.findElement(B ...

Is it possible to reuse a WebDriverWait instance?

When working with a page object that interacts with various elements on the DOM, is it better to create a single instance of WebDriverWait on initialization and use it for all waits? Or should separate instances be created for each element being waited on? ...

Selenium fails to function properly with Internet Explorer on a Remote Machine unless there is an active login session

Currently, I am utilizing Selenium IE WebDriver (version 2.46 latest) to conduct my tests on Internet Explorer. The setup consists of: Employing Jenkins to initiate my tests Running the tests and IE on a remote virtual machine An ongoing issue arises wh ...

Error occurs when Jackson attempts to serialize an object with Hibernate 5 module configured before it has been

Currently facing an issue with the Jackson FasterXML serialization to JSON when dealing with Lazily Initialized member collections of child elements in a grandchild level. Let's take a look at the entities involved: @Entity public class Parent { ...

Changing the dataURL of jqGrid dynamically after loading the Edit Form

Currently, I am working with jqGrid version 4.15.6-pre, which is the free version of jqGrid. Within my edit form, I have two dropdown lists that are being populated from the server using setColProp in the onSelectRow function. My objective is to reload t ...

Ways to discover the present date and transition to the following date?

Currently, I am encountering an issue with my automation script. My script utilizes the selenium tool along with Java language. The specific problem I am facing involves the need to view the current date after clicking on a calendar and verifying if a flig ...

Tips on how to interact with buttons of type <input> in C#

My attempts to click on the element using .css/by classname were unsuccessful... <div class="col-xs-12"> <input type="submit" name="commit" value="save" class="pp-btn pp-btn-primary w-70 save-item-description" data-disable-with="saving..."> ...

Unable to access code search functionality during the process of scraping from GitHub

Trying to create a simulation of the online GitHub search using Selenium web scraping. Struggling to make the program search within the Code section instead of repositories. Here is the code snippet: FirefoxProfile p = new FirefoxProfile(); p.setPref ...

The attempt to accomplish the task of executing goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile (default-testCompile) on the project has been

A few days ago, all my automation scripts were running smoothly both on Jenkins and locally. However, recently when I try to run the build on Jenkins or locally, I encounter an exception. Locally, if I update my Maven project and execute the command "maven ...

Adding Rust to an OpenJDK Docker image

I've been working on creating a dockerfile to run a rust test suite that needs a jar running on a separate port. I have the jar file stored in the project folder and plan to begin with an openjdk docker image, then download rust for testing purposes. ...

Error handling issues with multiple file uploads in Wicket: FileNotFoundException

We are encountering a unique issue in our Wicket 6+ application related to the FileUpload.writeTo(file) method. This error only occurs with specific file types (docx, xlsx, java) within one particular form. Interestingly, the identical code functions corre ...

The Selector object cannot be serialized into JSON format

Currently, I'm facing the challenge of scraping a dynamic website which requires the use of Selenium. The specific links that I'm interested in scraping only become visible when clicked on. These links are generated by jQuery, and there is no hr ...

Using arguments in cucumber scenarios

Is there any way to pass only one parameter in a cucumber step, even if the method expects two? Here is the code snippet: @Given("^skip the next scenario named \"(.*)\"$") @BeforeStep public void before(Scenario scenario, Stri ...

Executing identical Cucumber Features on multiple machines simultaneously with parallel_tests gem

Running identical Cucumber Features simultaneously on various machines using the parallel_tests gem I am currently exploring how to utilize the parallel_tests gem in order to execute the EXACT SAME Cucumber Features concurrently on different machines. At ...

Webdriver encounters difficulty locating elements using xpath or ID

After writing a code to automate signups for a sneakers raffle, I encountered a frustrating issue: the webdriver couldn't locate the elements on the page, which is quite perplexing. I've shared the link to the website and included a snippet of my ...

When attempting to upload multiple files in Spring using ng-file-upload, an empty List<MultipartFile> is encountered

My controller method for uploading multiple files at once is based on a blog post and answers to a question I found online: @RequestMapping(value = "/{user}/attachment", method = RequestMethod.POST) @PreAuthorize(...) public void upload(@PathVariable User ...