Steven asked:
I need to connect to a web site, download the home page, then search for a particular link within.
I need to connect to a web site, download the home page, then search for a particular link within.
I have achieved this, however the link I require is dynamically generated when the page loads using javascript, so my HTML parser in Java cannot see it.
If anyone knows how to load a HTML file and execute the javascript in Java, please let me know








Try to analyze the javascript which loads the link, maybe you can find a way of getting the link without running the actual script.
(I do not know how to run javascript in Java nor if its even possible)
Hi,
Take a look at the following code. Basic as it is, I think it may help you resolve your issues.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.undo.*;
import java.util.*;
import java.net.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
public class HTMLEditor extends JFrame
implements ActionListener,
DocumentListener, UndoableEditListener {
BorderLayout borderLayout1 =
new BorderLayout();
JPanel jPanel1 =
new JPanel();
JButton getFileBtn =
new JButton();
JButton closeBtn =
new JButton();
JTabbedPane jTabbedPane1 =
new JTabbedPane();
JScrollPane jScrollPane1 =
new JScrollPane();
JScrollPane jScrollPane2 =
new JScrollPane();
JEditorPane edText =
new JEditorPane();
JEditorPane edHTML =
new JEditorPane();
Document textDoc = null;
ArrayList undoList = new ArrayList();
JButton undoBtn = new JButton();
public HTMLEditor() {
try {
jbInit();
getFileBtn.addActionListener(
this);
undoBtn.addActionListener(this);
closeBtn.addActionListener(this);
}
catch(Exception e) {
e.printStackTrace();
}
}
public void loadFile(String fp) throws
Exception {
if (fp==null) return;
String filePath=fp;
File f=new File(fp);
long av=f.length();
if (av==0) {
System.out.println(no data);
return;
}
URL url=new URL(file:+fp);
edHTML.setPage(url);
FileReader fr=new FileReader(f);
edText.read(fr,null);
textDoc=this.edText.getDocument();
textDoc.addDocumentListener(this);
textDoc.addUndoableEditListener(
this);
this.validate();
}
public static void main(String[] args) {
HTMLEditor ed = new HTMLEditor();
ed.setBounds(20,20,500,400);
ed.setVisible(true);
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(
borderLayout1);
getFileBtn.setText(Get FIle);
closeBtn.setText(Close);
edText.setText(jEditorPane1);
edHTML.setEditable(false);
edHTML.setText(jEditorPane2);
undoBtn.setEnabled(false);
undoBtn.setText(Undo);
this.getContentPane().add(
jPanel1, BorderLayout.SOUTH);
jPanel1.add(getFileBtn, null);
jPanel1.add(undoBtn, null);
jPanel1.add(closeBtn, null);
this.getContentPane().add(
jTabbedPane1, BorderLayout.CENTER);
jTabbedPane1.add(jScrollPane1,
HTML);
jScrollPane1.getViewport().add(
edHTML, null);
jTabbedPane1.add(
jScrollPane2, Text);
jScrollPane2.getViewport().add(
edText, null);
addWindowListener(new WindowAdapter() {
public void windowClosing(
WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(
ActionEvent e) {
if (e.getSource() == this.closeBtn)
System.exit(0);
if (e.getSource() == this.getFileBtn) {
FileDialog fd=new FileDialog(
this,Select File,FileDialog.LOAD);
fd.setVisible(true);
String fName=fd.getFile();
if (fName==null) return;
String fPath=fd.getDirectory()+fName;
try {
loadFile(fPath);
}
catch (Exception ex) {
System.out.println(
ex.getMessage());
ex.printStackTrace();
}
} else if (
e.getSource() == this.undoBtn) {
undoLast();
}
}
public void undoLast() {
int ix=undoList.size()-1;
if (ix=0) {
UndoableEdit ue =
(UndoableEdit
)this.undoList.get(ix);
ue.undo();;
undoList.remove(ix);
if (undoList.size()==0) this.undoBtn.setEnabled(false);
}
}
public boolean isFileChanged() {
return this.undoList.size()0;
}
public void insertUpdate(DocumentEvent e) {
System.out.println(insertUpdate);
}
public void removeUpdate(DocumentEvent e) {
System.out.println(removeUpdate);
}
public void changedUpdate(DocumentEvent e) {
System.out.println(
changedUpdate);
}
public void undoableEditHappened(
UndoableEditEvent e) {
this.undoList.add(e.getEdit());
this.undoBtn.setEnabled(true);
}
}
Regards,
TgTips