1

I am automating a process a process where I need to download csv files from a website.

I have written a jquery code for that. Can someone tell me how can i integrate this code with my Java selenium code.

a.java

package package1;

import java.text.ParseException;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.seleniumhq.jetty9.util.log.Log;

public class Noofrowsandcols {
public static void main(String[] args) throws ParseException, InterruptedException {
WebDriver wd;
String exepath="C:\\Users\\sh370472\\Downloads\\chromedriver_win32 (2)\\chromedriver.exe";
 System.setProperty("webdriver.chrome.driver", exepath);
 wd= new ChromeDriver();
 wd.get("https://www.shipper-ml.com");
 wd.findElement(By.id("inpUserId")).sendKeys("xxxxxxxx");
 wd.findElement(By.id("inpPassword")).sendKeys("xxxxxxxxxxxxxx");
 wd.findElement(By.id("btnLogonLabel")).click();
 Thread.sleep(1000);
 wd.get("https://www.shipper-ml.com/viewReports.do");  

 }

}

b.js

$(document).ready(function () {
$(window).load(function () {
$('.ibody tr').each(function (a, b) {
var count=0;
var name = $('.cl', b).text();
if(name.indexOf(".CSV")!==-1 && name.indexOf("TAS")!==-1){
var d= a-9;
var hiddenIFrameID = 'hiddenDownloader' + count++;
var iframe = window.createElement('iframe');
iframe.id = hiddenIFrameID;
iframe.style.display = 'none';
window.body.appendChild(iframe);
iframe.src = "https://www.shipper-ml.com/viewReports.do? 
ctrl=reportListForDownload&action=DownloadReport&param="+d;

}
});

}); });

 JavascriptExecutor js = (JavascriptExecutor) wd;
 js.executeScript("$(document).ready(function () {
 $(window).load(function () {
 $('.ibody tr').each(function (a, b) {
 var count=0;
  var name = $('.cl', b).text();
  if(name.indexOf(".CSV")!==-1 && name.indexOf("TAS")!==-1){
  var d= a-9;
  var hiddenIFrameID = 'hiddenDownloader' + count++;
  var iframe = window.createElement('iframe');
  iframe.id = hiddenIFrameID;
  iframe.style.display = 'none';
  window.body.appendChild(iframe);
  iframe.src = "https://www.shipper-ml.com/viewReports.do?ctrl=reportListForDownload&action=DownloadReport&param="+d;

      }
        });
    });

}); ")));

1

Since jQuery is a library, to run jQuery hmm.. queries.. you have to have it embedded to your page first of all. See the details here. That basically means that you cannot take just any page and run jQuery-based code there.

After you have made sure your page loads jQuery library, you can execute jQuery javascript in a regular way that Selenium provides. Find the details here.

  • My Jquery code runs perfectly in inspect console of that website page. – Novice Nov 28 '18 at 10:58
  • Great! Then use JavascriptExecutor to run your code. – Alexey R. Nov 28 '18 at 10:59
  • Can you please help me with it as I don't know how to integrate jquery using JavascriptExecutor ? – Novice Nov 29 '18 at 5:08
  • I have updated the code with JavascriptExecutor ... Can you please tell me if it's correct or should I do something else also ? – Novice Nov 29 '18 at 5:36
  • Your script is a string. You should put your script into a String object. Basically that means to wrap your script into double quotes. – Alexey R. Nov 29 '18 at 11:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.