[英]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. 我有一些测试用例来验证使用selenium的下载文件。在那里,我可以自动处理浏览器弹出窗口并提供要下载文件的默认路径。
Handle download popup automatically in Chrome 在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. 这在我的本地机器上可以正常工作,但是由于上述代码段的一些限制,所以在selenium网格中不起作用。作为解决方案,我模仿了浏览器cookie状态并抓取文件URL并以编程方式将文件写入本地位置,如本例所示 。在这种方法上有一些问题。 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. 因为它要求Web元素包含一个
href
属性才能找到下载文件的URL。在mycase中,它将在单击按钮时调用Java脚本函数以生成文件URL。
HTML code snippet HTML代码段
<input type="button" onclick="javascript:downloadFile()" value="Download">.//does not contains href
Js code snippet Js代码段
<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? 请让我知道如何获取文件URL以编程方式下载文件。是否有任何浏览器侦听器触发按钮单击并获取文件URL?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.