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;
 }
 });
 });

}); ")));

asked Nov 28, 2018 at 4:04

1 Answer 1

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.

answered Nov 28, 2018 at 10:48
6
  • My Jquery code runs perfectly in inspect console of that website page. Commented Nov 28, 2018 at 10:58
  • Great! Then use JavascriptExecutor to run your code. Commented Nov 28, 2018 at 10:59
  • Can you please help me with it as I don't know how to integrate jquery using JavascriptExecutor ? Commented Nov 29, 2018 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 ? Commented Nov 29, 2018 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. Commented Nov 29, 2018 at 11:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.