Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to search YouTube with HtmlUnit
    text
    copied!<p>I wonder if YouTube could be searched with <a href="http://htmlunit.sourceforge.net/" rel="nofollow">HtmlUnit</a>. I started to write code, here it is:</p> <pre class="lang-java prettyprint-override"><code>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 is equivalent to typing youtube.com to the adress bar of browser HtmlPage currentPage = webClient.getPage("http://www.youtube.com"); //Get form where submit button is located HtmlForm searchForm = (HtmlForm) currentPage.getElementById("masthead-search"); //Printing result form System.out.println(searchForm.asText()); final List&lt;HtmlAnchor&gt; listLinks = (List&lt;HtmlAnchor&gt;) newPage.getByXPath("//a[@class='ux-thumb-wrap result-item-thumb']"); for (int i=0; i&lt;listLinks.size(); i++){ System.out.println(YOUTUBE + listLinks.get(i).getAttribute("href")); } } } </code></pre> <p>Now I don't know how to type some text into a search field and press Search button.</p> <p>I saw tutorials about HtmlUnit but I'm having a problem because they use a method named: <code>getElementByName</code> but the search button on YouTube doesn't have a name, just an id. Could someone help me?</p> <p>EDIT: I edited code above code and now I am getting youtube links from first page. But before that I need to sort by upload date and then to grab links. Can someone help me to do sorting?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload