Results 1 to 8 of 8
Thread: Cast error
- 04-21-2011, 10:41 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
Cast error
Hi,
First off im new here and fairly new to java scripting. So try and take it easy. :o
The issue im having is i keep getting a cast error when running the code below. i am using HtmlUnit to try and submit a form online for me. here is the error:
and here is my code:Exception in thread "main" java.lang.ClassCastException: com.gargoylesoftware.htmlunit.html.HtmlButtonInput cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlSubmitInput
at formfillerbot.fill(formfillerbot.java:24)
at formfillerbot.main(formfillerbot.java:30)
The xxxxxx are taking up name slots and other items which i know are valid. Any help you could give would be great!Java Code:import com.gargoylesoftware.htmlunit.Page; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;; public class formfillerbot { private String bUrl; public formfillerbot (String url) throws Exception { bUrl = url; } public void fill () throws Exception{ WebClient wb = new WebClient (); Page p = (HtmlPage) wb.getPage(bUrl); HtmlForm f = ((HtmlPage) p).getFormByName("xxxxxxxx"); HtmlRadioButtonInput r = f.getElementById("xxxxxxxxxx"); r.click(); HtmlSubmitInput s = (HtmlSubmitInput) f.getInputByName("xxxxxxxxx"); System.out.println(s.asText()); } public static void main (String args[]) throws Exception { formfillerbot xyro = new formfillerbot ("http://xxxxxxxx.xxxxxx"); xyro.fill (); } }
Nat
- 04-21-2011, 11:27 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Exception in thread "main" java.lang.ClassCastException
This means that you are performing a cast and, at runtime, the object you are trying to cast is not of the type you are trying to cast to. Specifically you say
Java Code:f.getInputByName("xxxxxxxxx");
which returns some sort of input. But what sort of input - text, button, submit, etc? That is a question that can only be answered at runtime because it depends on what sort of form element actually has the name "xxxxxxxxx".
It turns out that for the actual web page you are looking at what is returned is an instance of HtmlButtonInput but is not an instance of HtmlSubmitInput. Hence the exception.
-----
You can check the source of the web page to see what actually does have the name "xxxxxxxxx". It is quite possible for a form to provide a button that looks and feels like a submit button but which really isn't: rather it runs some JavaScript which does whatever is required and then (possibly) sends data to the server.
- 04-22-2011, 05:09 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
ok, so say the form submit is this here:
Which executes JavaScript, so i need to store that as a button not a submit button, that way when it is clicked on by say this code:Java Code:<input id="IE_Last" name="IE_Last" type="button" class="button_participate" value="Submit" onclick="return GoToPg('NXT_del')">
it will be click and submit the form correct?Java Code:HtmlButton s = f.getElementById("IE_Last"); s.click();
- 04-22-2011, 05:33 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I don't know - I've never used com.gargoylesoftware.htmlunit, perhaps someone else here has. Or you can try it and see. But the error message was saying that an HtmlButtonInput was being returned, not an HtmlButton as you are saying in that code.
- 04-22-2011, 05:36 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
im thinking that because the types are the same it will work. the website is down for maintenance for a while so i cant do anything right now, im getting the rest of my code set up, loops and what not. If i need more help or if it works ill reply! thanks for the explanation!
- 04-25-2011, 09:59 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
Hi again,
so i got everything to work and have a swing timer implemented. im trying to do something that is probably very simple but i dont understand. I'm trying to pass the username the bot votes with to the timer JFrame popup with countdown timer. How can i pass the variables username and votingip throughout the code?
Thanks!Java Code:import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.Page; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlParagraph; import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput; import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; public class voting_bot extends JFrame { private String bUrl; public String username; JLabel label; JLabel userlabel; Timer votetimer; // Initial game time int timeRemaining = 180; static int pausetime = 180; public voting_bot() { setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(200, 200); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3, 1)); userlabel = new JLabel(String.valueOf(username), JLabel.CENTER); userlabel.setVerticalAlignment(JLabel.TOP); label = new JLabel(String.valueOf(timeRemaining), JLabel.CENTER); panel.add(userlabel); panel.add(label); votetimer = new Timer(1000, new votetimerListener()); setVisible(true); add(panel); votetimer.start(); } class votetimerListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (--timeRemaining > 0) { label.setText(String.valueOf(timeRemaining)); } else { label.setText("Time's up!"); votetimer.stop(); dispose(); } } } public String randomuser () throws Exception{ //random user Random randomizer = new Random(); int randomNumber2 = randomizer.nextInt(3); String user = null; if (randomNumber2 == 0){ user = "xxx"; //Sophomore } else if (randomNumber2 == 1) { user = "xx"; //Junior } else if (randomNumber2 == 2) { user = "x"; //Parent } return user; } public voting_bot (String url) throws Exception { bUrl = url; } public void vote () throws Exception{ String proxyip = "173.0.58.178"; //String proxyip = "128.220.231.4"; int proxyport = 3128; //WebClient wb = new WebClient (BrowserVersion.FIREFOX_3, proxyip, proxyport); WebClient wb = new WebClient (BrowserVersion.FIREFOX_3); wb.setCssEnabled(false); wb.setJavaScriptEnabled(false); Page p = (HtmlPage) wb.getPage(bUrl); String user = randomuser(); if (user == "xxx"){ username = "Sophmore"; //Sophomore } else if (user == "xx") { username = "Junior"; //Junior } else if (user == "x") { username = "Parent"; //Parent } HtmlForm f = ((HtmlPage) p).getFormByName("form_info"); HtmlRadioButtonInput r = f.getElementById("xxxx"); HtmlRadioButtonInput ru = f.getElementById(user); HtmlSubmitInput s = (HtmlSubmitInput) f.getElementById("BUTTON_SUBMIT"); r.click(); ru.click(); HtmlPage rp = (HtmlPage) s.click(); System.out.println(rp.asText()); System.out.println(username); Page ipp = (HtmlPage) wb.getPage("xxxxxxxx"); HtmlParagraph ip = (HtmlParagraph) ((HtmlPage) ipp).getHtmlElementById("ip"); String votingip = ip.asText(); System.out.println(votingip); System.out.println(""); System.out.println("*****************************************************************************************"); System.out.println(""); //final HtmlPage iptestp = (HtmlPage) wb.getPage("http://www.sjspecialties.com/Scripts/email_browser_detection.php"); } public static void main(String[] args) throws Exception { voting_bot vb = new voting_bot ("xxxxxxxxxxx"); for (;;){ vb.vote (); EventQueue.invokeLater(new Runnable() { public void run() { new voting_bot(); } }); Thread.sleep(pausetime*1000); } } }
- 04-26-2011, 12:48 AM #7
Member
- Join Date
- Apr 2011
- Location
- Canada!
- Posts
- 30
- Rep Power
- 0
I never used HTMLunit so I can't help you with that, but if you want a great tool for web UI automation, look at Selenium.
- 04-26-2011, 02:48 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Problem with my cast
By flypadre in forum New To JavaReplies: 4Last Post: 01-20-2011, 02:13 PM -
Dynamic cast
By Vizualni in forum AWT / SwingReplies: 2Last Post: 08-17-2010, 06:21 PM -
Cannot cast JNLPAppletLauncher to itself
By kingofearth in forum Java AppletsReplies: 0Last Post: 01-19-2010, 07:37 PM -
Cast Error Caught (change) Class is really: java.lang.String
By barney in forum Advanced JavaReplies: 1Last Post: 08-02-2007, 04:07 PM -
How can I cast Object as an int
By romina in forum New To JavaReplies: 1Last Post: 07-18-2007, 11:20 AM


LinkBack URL
About LinkBacks

Bookmarks