Results 1 to 8 of 8

Thread: Cast error

  1. #1
    nat45928 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default 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:

    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)
    and here is my code:

    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 ();
    	 }
    	}
    The xxxxxx are taking up name slots and other items which i know are valid. Any help you could give would be great!

    Nat

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  3. #3
    nat45928 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    ok, so say the form submit is this here:

    Java Code:
    <input id="IE_Last" name="IE_Last" type="button" class="button_participate" value="Submit" onclick="return GoToPg('NXT_del')">
    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:
    	  HtmlButton s = f.getElementById("IE_Last");
    	  s.click();
    it will be click and submit the form correct?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  5. #5
    nat45928 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    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!

  6. #6
    nat45928 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    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?

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

  7. #7
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    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.

  8. #8
    nat45928 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    basically what i need help with now is how to pass variables from the vote() method to the voting_bot() method. Selenium does look interesting though......

Similar Threads

  1. Problem with my cast
    By flypadre in forum New To Java
    Replies: 4
    Last Post: 01-20-2011, 02:13 PM
  2. Dynamic cast
    By Vizualni in forum AWT / Swing
    Replies: 2
    Last Post: 08-17-2010, 06:21 PM
  3. Cannot cast JNLPAppletLauncher to itself
    By kingofearth in forum Java Applets
    Replies: 0
    Last Post: 01-19-2010, 07:37 PM
  4. Replies: 1
    Last Post: 08-02-2007, 04:07 PM
  5. How can I cast Object as an int
    By romina in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 11:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •