java - How to search YouTube with HtmlUnit -


i wonder if youtube searched htmlunit. started write code, here is:

import java.io.ioexception; import java.net.malformedurlexception; import com.gargoylesoftware.htmlunit.failinghttpstatuscodeexception; import com.gargoylesoftware.htmlunit.webclient; import com.gargoylesoftware.htmlunit.html.htmlform; import com.gargoylesoftware.htmlunit.html.htmlpage; import com.gargoylesoftware.htmlunit.html.htmlsubmitinput;  public class htmlunitexampletestbase {     private static final string youtube = "http://www.youtube.com";     public static void main(string[] args) throws failinghttpstatuscodeexception, malformedurlexception, ioexception {         webclient webclient = new webclient();         webclient.setthrowexceptiononscripterror(false);          //this equivalent typing youtube.com adress bar of browser         htmlpage currentpage = webclient.getpage("http://www.youtube.com");          //get form submit button located         htmlform searchform = (htmlform) currentpage.getelementbyid("masthead-search");         //printing result form         system.out.println(searchform.astext());         final list<htmlanchor> listlinks = (list<htmlanchor>) newpage.getbyxpath("//a[@class='ux-thumb-wrap result-item-thumb']");         (int i=0; i<listlinks.size(); i++){             system.out.println(youtube + listlinks.get(i).getattribute("href"));         }     }    } 

now don't know how type text search field , press search button.

i saw tutorials htmlunit i'm having problem because use method named: getelementbyname search button on youtube doesn't have name, id. me?

edit: edited code above code , getting youtube links first page. before need sort upload date , grab links. can me sorting?

i'm no htmlunit expert, there workaround. can add own button form , use submit form.

here's code sample comments:

import java.io.ioexception; import java.net.malformedurlexception;  import com.gargoylesoftware.htmlunit.failinghttpstatuscodeexception; import com.gargoylesoftware.htmlunit.webclient; import com.gargoylesoftware.htmlunit.html.htmlbutton; import com.gargoylesoftware.htmlunit.html.htmlform; import com.gargoylesoftware.htmlunit.html.htmlpage; import com.gargoylesoftware.htmlunit.html.htmltextinput;  public class htmlunitexampletestbase {    public static void main(string[] args) throws failinghttpstatuscodeexception, malformedurlexception, ioexception {       webclient webclient = new webclient();       webclient.setthrowexceptiononscripterror(false);        // equivalent typing youtube.com adress bar of browser       htmlpage currentpage = webclient.getpage("http://www.youtube.com");        // form submit button located       htmlform searchform = (htmlform) currentpage.getelementbyid("masthead-search");        // input field.       htmltextinput searchinput = (htmltextinput) currentpage.getelementbyid("masthead-search-term");       // insert search term.       searchinput.settext("nyan cat");        // workaround: create 'fake' button , add form.       htmlbutton submitbutton = (htmlbutton) currentpage.createelement("button");       submitbutton.setattribute("type", "submit");       searchform.appendchild(submitbutton);        // workaround: use reference button submit form.        htmlpage newpage = submitbutton.click();        system.out.println(newpage.astext());    } } 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -