Can we give bulk url's in this code? - javascript

URL url = new URI("http://tamilblog.ishafoundation.org/").toURL();
I want to give 100 url in this connector?
I have the code to run single home page to extract its data
I need to get more contents From several links !
Is it any way possible ?
Here is my code
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class NewClass {
public static void main(String[] args) throws IOException {
Document doc = Jsoup.connect("http://tamilblog.ishafoundation.org").get();
Elements section = doc.select("section#content");
Elements article = section.select("article");
for (Element a : article) {
System.out.println("Title : \n" + a.select("a").text());
System.out.println("Article summary: \n" + a.select("div.entry-summary").text());
}
}
}
I got output from this single page Now i need to get it from several pages (100+links) at once.
And to save it in a document.

Related

Print multiple texts from multiple elements with same class name

Im trying to print all the product names from an e commerce website in selenium using java but its printing only the first name of the product from the first class it finds. How to print all the product names from all the classes with same class name?
This is my code:
package introduction;
import java.time.Duration;
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;
public class Getproductnames {
private static int i;
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "D:/Temp/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.get("https://www.fipola.in/chicken");
driver.findElement(By.id("DelLocation")).sendKeys("600020");
driver.findElement(By.className("top_pincode_select")).click();
Thread.sleep(3000);
List<WebElement> products=driver.findElements(By.cssSelector("a.product-item-link"));
for(int i=0; i<products.size(); i++);
{
String[] names = new String[]{products.get(i).getText()};
System.out.println(names[i] + "");
}
}
}
You can use the below to print all the products name
List<WebElement> products=driver.findElements(By.cssSelector("a.product-item-link"));
for(int i =0;i<products.size();i++) {
String elementText = products.get(i).getText();
System.out.println(elementText);
}

How to check when the JS has changed HTML upon AJAX response?

We try to scrape a website using Selenium, JAVA, eg. try to change product color at https://www.ergodyne.com/shax-6054-pop-up-tent-sidewall-kit-includes-10ftx10ft
How to find out/check when the in-site JS has changed the HTML upon the arrival of the AJAX response ?
How big should be delay after AJAX/XHR response and JS replaced HTML ?
The AJAZ queries are quite complicated to try to get data from them...
Link for JSON response: https://jsonformatter.org/298d31
Using org.openqa.selenium.support.ui.ExpectedConditions and org.openqa.selenium.support.ui.WebDriverWait. See https://www.guru99.com/implicit-explicit-waits-selenium.html
Code:
package tests;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class IgorSavinking {
public static String userDir = System.getProperty("user.dir");
public static String chromedriverPath = userDir + "\\resources\\chromedriver.exe";
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", chromedriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--ignore-certificate-errors");
options.addArguments("--start-maximized");
options.addArguments("--disable-notifications");
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.ergodyne.com/shax-6054-pop-up-tent-sidewall-kit-includes-10ftx10ft");
driver.findElement(By.id("cookie-policy-accept")).click();
String colorBefore = driver.findElement(By.tagName("legend")).getText().substring(5);
System.out.println("Current color is: " + colorBefore);
driver.findElement(By.xpath("//label[contains(#for,'edit-purchased-entity-0-attributes-attribute-color-17')]")).click();
new WebDriverWait(driver, 30).until(ExpectedConditions.numberOfElementsToBe(By.xpath("div[#class='message']"), 0));
String colorAfter = driver.findElement(By.tagName("legend")).getText().substring(5);
System.out.println("Current color is: " + colorAfter);
System.out.println("Color has been changed: " + !colorBefore.equals(colorAfter));
driver.quit();
}
}
Output:
Starting ChromeDriver 97.0.4692.71 (adefa7837d02a07a604c1e6eff0b3a09422ab88d-refs/branch-heads/4692#{#1247}) on port 38237
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Úno 02, 2022 8:02:15 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Current color is: Lime
Current color is: Blue
Color has been changed: true

Programming string to array/list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
So I received a text file to create an API, and in this text file I have an example similar to this:
id,first_name,last_name
1,John,Smith
2,Charlie,Sheen
The list goes on with about 100+ rows.
Is there a way to organize this programmatically?
I don't want to manually write this into json format.
I'll take java, C#, Javascript, or php methods.
public String[] returnArray(String line){
return line.split(",");}
This organizes the new array into the elements between each comma. In this example the first element would equal id.
There are lots of open source CSV readers out there for Java and other languages and I highly recommend using one of them. However, if you want a quickly working solution with no additional libraries you can use the following code. Make sure to edit the file name to point to wherever your text file is. Or you can edit the code to allow someone to pass the file name in. When you run this it will echo out the contents of the file.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadFile
{
private static class Record
{
public String id;
public String firstName;
public String lastName;
#Override
public String toString()
{
return id + "," + firstName + "," + lastName;
}
}
public static void main(String[] args) throws IOException
{
List<Record> records = getRecordsFromFile("C:/Myfile.txt");
for(Record record : records)
{
System.out.println(record);
}
}
private static List<Record> getRecordsFromFile(String fileName) throws IOException
{
List<Record> records = new ArrayList<>();
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(new File(fileName)));
String line = null;
while((line = reader.readLine()) != null)
{
records.add(makeRecordFromLine(line));
}
return records;
}
finally
{
if(reader != null)
{
reader.close();
}
}
}
private static Record makeRecordFromLine(String line)
{
String[] lineArray = line.split(",");
Record record = new Record();
record.id = lineArray[0];
record.firstName = lineArray[1];
record.lastName = lineArray[2];
return record;
}
}
you can use past special techniques to read all the json please have a look into this link:
How to show the "paste Json class" in visual studio 2012 when clicking on Paste Special?

Jasper Report : "Document has no pages"

I have a problem when I run this code. I get always this message : "Document has no pages" with a blank page in the pdf file .
Note: I'm using NetBeans IDE 7.2 Beta.
Jar files imported are:
com.lowagie.text-2.1.7.jar
commons-beanutils-1.5.jar
commons-collections-2.1.jar
commons-digester-2.1.jar
commons-javaflow.jar
commons-logging-1.3.jar
jasperreports-5.5.1.jar
javax.servlet.jar
I need your help,please.
this is my code :
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Currency;
import java.util.HashMap;
import javax.swing.JFrame;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.swing.JRViewer;
public class Report extends JFrame{
public Report(String month,int year){
try{
//load the driver
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/motor";
String user="root";
String pass="";
Connection cn=DriverManager.getConnection(url,user,pass);
System.out.println("connected in report");
PreparedStatement ps = cn.prepareStatement("select number,title,name,ikar,echtirak,price,tarakom from users order by number asc");
ResultSet rs;
rs=ps.executeQuery();
JasperPrint jasperPrint=null;
HashMap<String, Object> mapParameters = new HashMap <String, Object>();
// jrxml compiling process
JasperReport jasperReport = JasperCompileManager.compileReport("C:/Users/user/Documents/NetBeansProjects/MotorApp/MyReports/motorReport.jrxml");
int i=1;
while(rs.next()){
mapParameters.put("Name", rs.getString("title")+" "+rs.getString("name"));
mapParameters.put("Number",rs.getString("number"));
mapParameters.put("Ikar",rs.getString("ikar"));
mapParameters.put("Amperage",rs.getString("echtirak")+" A");
mapParameters.put("tarakom",rs.getString("tarakom")+" $");
mapParameters.put("monthPrice",rs.getString("price")+" $");
Integer total=Integer.parseInt(rs.getString("price"))+Integer.parseInt(rs.getString("tarakom"));
mapParameters.put("totalPrice",total+" $");
mapParameters.put("monthYear",year+" "+month);
try{
System.out.println("Filling report...");
jasperPrint = JasperFillManager.fillReport(jasperReport, mapParameters,cn);
JRViewer viewer = new JRViewer(jasperPrint);
System.out.println("Done!");
JasperExportManager.exportReportToPdfFile(jasperPrint,"C:/Users/user/Desktop/"+month+" "+year+".pdf");
}catch(JRException elle){
System.out.println(elle.getMessage());
}
}
}catch(Exception ev){
System.out.println(ev.getMessage());
}
}
}
I truly appreciate your help.
My first thought is on the motorReport.jrxml file. It may have some issues on its content. Try reducing the contents of the report to a point where the code works and the reports can display properly. It may give you a clue to the cause of the problem. Then get back here with more specifics if you still can't solve this problem.
mapParameters in your code is not records of DataSource. It is parameters of report, therefore report haven't records.
One of the possible solutions: convert ResultSet into BeanCollection, make JRBeanCollectionDataSource and pass it into fillReport.
Make bean for saving result of query.
public class UserBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
....
}
For example result is
List myList = new ArrayList<UserBean>();
Convert into collection example here stackoverflow.com/questions/17206523/put-resultset-values-into-collection-object-then-add-to-arraylist
After convert you must call fillReport like this:
jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap<String,Object>, new JRBeanCollectionDataSource(myList));

How to call phone plugin in javascript

Ok I am trying to implement an phone gap plugin, that consists of two parts. I am using cordova 2.0.0 and eclipse.
Here is the java part:
package org.apache.cordova;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import android.graphics.Bitmap;
import android.os.Environment;
import android.view.View;
public class Screenshot extends Plugin {
#Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
// starting on ICS, some WebView methods
// can only be called on UI threads
final Plugin that = this;
final String id = callbackId;
super.cordova.getActivity().runOnUiThread(new Runnable() {
//#Override
public void run() {
View view = webView.getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
try {
File folder = new File(Environment.getExternalStorageDirectory(), "Pictures");
if (!folder.exists()) {
folder.mkdirs();
}
File f = new File(folder, "screenshot_" + System.currentTimeMillis() + ".png");
FileOutputStream fos = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
that.success(new PluginResult(PluginResult.Status.OK), id);
} catch (IOException e) {
that.success(new PluginResult(PluginResult.Status.IO_EXCEPTION, e.getMessage()), id);
}
}
});
PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
return result;
}
}
Here is the javascript part:
cordova.define("cordova/plugin/screenshot", function(require, exports, module) {
var exec = require('cordova/exec');
/**
* This class exposes the ability to take a Screenshot to JavaScript
*/
var Screenshot = function() {};
/**
* Save the screenshot to the user's Photo Library
*/
Screenshot.prototype.saveScreenshot = function() {
exec(null, null, "Screenshot", "saveScreenshot", []);
};
var screenshot = new Screenshot();
module.exports = screenshot;
});
if (!window.plugins) {
window.plugins = {};
}
if (!window.plugins.screenshot) {
window.plugins.screenshot = cordova.require("cordova/plugin/screenshot");
}
I am trying to call this with another javascript function on another page, but without success. I hide the anchors of an image on a canvas, then this line:
setTimeout(takeScreenShot,500);
EDIT -- made after Simon MacDonald's answer
this then relates to a javascript function:
function takeScreenShot() {
window.plugins.screenshot.saveScreenshot();
}
The following java prints:
System.out.println(folder);
System.out.println("screenshot_" + System.currentTimeMillis() + ".png");
Produce the following results:
/mdt/sdcard/Pictures
screenshot_1347893081276.png
EDIT After turning the device off and on again, the screenshots I took appeared, the phone seems to cache them, and not actually store them to the selected folder.
I have ensured that my config.xml and my android manifest have the right permissions and lines of code. Anyone see where im going wrong?
nowhere in your code do you ever call the saveScreenshot method. Your takeScreenShot method should look like this:
function takeScreenShot() {
window.plugins.screenshot.saveScreenshot();
}
Then the screen shot should be saved in "/sdcard/Pictures". This will work if:
You remembered to put a plugin line into your config.xml for the screen shot plugin
You mentioned this is on another page so make sure that page has a script tag for screenshot.js.
Did you try with the Obsolute Path?
File sdCard = Environment.getExternalStorageDirectory();
File folder = new File (sdcard.getAbsolutePath() + "Pictures");

Categories

Resources