简体   繁体   中英

How can i get the file URL from web element if it calls java script function in Selenium

I am having some test cases to verify download files using selenium.In there i have handle browser popups automatically and gave a default path to file to be downloaded.

Handle download popup automatically in Chrome

ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("download.default_directory","/downloadpath/");
options.setExperimentalOption("prefs", prefs);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);

This works fine in my local machine.but due to some restrictions above code snippet dont work in selenium grid.As a solution i mimic the browser cookie state and grab the file URL and programmatically write the file into local location like in this example .But having some problem in this approach. Because it requires web element to contains a href attribute to locate the download file URL.In mycase it will calls the java script function on button click to generate the file URL.

HTML code snippet

<input type="button" onclick="javascript:downloadFile()" value="Download">.//does not contains href

Js code snippet

<SCRIPT language="javascript">   
    function downloadFile() {
       var fileType = document.form.fileType.options[document.form.fileType.selectedIndex].value;
          var boType = "TransportManifest"
             if(fileType == null || fileType == "") {
                window.alert('Please select a file type');
            } else {
                 var format = fileType.substring(0, fileType.indexOf("~~"));
                 var link = "/local/dynamic1234." + format + "?key="+154220756+"&producer=ExportMediaProducer"; // i need to access this generated URL
                 link = link + "&fileType=" + fileType + "&type=" + boType;
                document.location=link;                    
                }
                 } 
      </SCRIPT>

Please let me know how can i get the file URL to programmatically download the file.Is there any browser listeners to trigger button click and grab the file URL?

when you click on the element it's suppose to create new frame with the href you need try to inspect the elements after the click.

if it's didn't help download FIDDLER

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM