简体   繁体   English

如果它调用Selenium中的Java脚本函数,如何从Web元素获取文件URL

[英]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?

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. 当您单击元素时,假设要使用href创建新框架,则需要在单击后尝试检查元素。

if it's didn't help download FIDDLER 如果没有帮助下载FIDDLER

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从java脚本广告行获取网址? - How can I get url from java script ad line? 我如何从 java 脚本中的 URL 获得价值 - How can i get value from URL in java script 将硒与python结合使用,如何从JS中声明的HTML中获取Var <script> element - Using selenium with python, how can I get Var from HTML where it's declared in a JS <script> element 如何从 Java 脚本获取 Json 文件数组 - How Can I Get a Json File Array From Java Script 如何从Firebug Profiler中获取函数调用的参数? - How Can I Get the Arguments of Function Calls from the Firebug Profiler? 如何获得调用我的函数的元素的ID,类或HTML标签? - How can I get the ID, class or HTML tag of an element that calls my function? 如何使用java脚本从函数内部获取值? - how can i get a value from inside a function using java script? 我如何从批处理文件调用具有2个参数的Java脚本函数并将结果返回到dos变量 - how I can call from batch file a java script function with 2 arguments and return the result to a dos variable 如何捕获 Web 元素并将其重用于 selenium 中的测试 - how can I capture web element and reuse it to for test in selenium “如何在 javascript 中获取 google apps 脚本 web 应用沙箱的 url 查询字符串参数? - "How can I get the url query string parameters of a google apps script web app sandbox in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM