Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6172361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:22:47+00:00 2026-05-23T23:22:47+00:00

import java.io.IOException; import java.net.MalformedURLException; import java.util.List; import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlAnchor; import com.gargoylesoftware.htmlunit.html.HtmlButton;

  • 0
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
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 YoutubeBot {
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/results?search_type=videos&search_query=official+music+video&search_sort=video_date_uploaded&suggested_categories=10%2C24&uni=3");

    // Get form where submit button is located
    HtmlForm searchForm = (HtmlForm) currentPage.getElementById("masthead-search");

    // Get the input field.
    HtmlTextInput searchInput = (HtmlTextInput) currentPage.getElementById("masthead-search-term");
    // Insert the search term.
    searchInput.setText("java");

    // Workaround: create a 'fake' button and add it to the form.
    HtmlButton submitButton = (HtmlButton) currentPage.createElement("button");
    submitButton.setAttribute("type", "submit");
    searchForm.appendChild(submitButton);

    //Workaround: use the reference to the button to submit the form. 
    HtmlPage newPage = submitButton.click();

    //Find all links on page with given class
    final List<HtmlAnchor> listLinks = (List<HtmlAnchor>) currentPage.getByXPath("//a[@class='ux-thumb-wrap result-item-thumb']");      

    //Print all links to console
    for (int i=0; i<listLinks.size(); i++)
        System.out.println(YOUTUBE + listLinks.get(i).getAttribute("href"));

    }
}

This code is working but I just want to sort youtube clips for example by upload date. How to do this with HtmlUnit? I have to click on filter, this should load content by ajax request and then I should click on “Upload date” link. I just don’t know this first step, to load ajax content. Is this possible with HtmlUnit?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:22:47+00:00Added an answer on May 23, 2026 at 11:22 pm

    Here’s one way to do it:

    1. Search the page as you did in your previous question.
    2. Select search-lego-refinements block by id.
    3. Use XPath to navigate to the URL (//ul/li/a when you start from the previous id).
    4. Click the selected link.

    The following code sample shows how it could be done:

    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.util.List;
    
    import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
    import com.gargoylesoftware.htmlunit.WebClient;
    import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
    import com.gargoylesoftware.htmlunit.html.HtmlButton;
    import com.gargoylesoftware.htmlunit.html.HtmlElement;
    import com.gargoylesoftware.htmlunit.html.HtmlForm;
    import com.gargoylesoftware.htmlunit.html.HtmlPage;
    import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
    
    public class YoutubeBot {
       private static final String YOUTUBE = "http://www.youtube.com";
    
       @SuppressWarnings("unchecked")
       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(YOUTUBE);
    
          // Get form where submit button is located
          HtmlForm searchForm = (HtmlForm) currentPage.getElementById("masthead-search");
    
          // Get the input field
          HtmlTextInput searchInput = (HtmlTextInput) currentPage.getElementById("masthead-search-term");
    
          // Insert the search term
          searchInput.setText("java");
    
          // Workaround: create a 'fake' button and add it to the form
          HtmlButton submitButton = (HtmlButton) currentPage.createElement("button");
          submitButton.setAttribute("type", "submit");
          searchForm.appendChild(submitButton);
    
          // Workaround: use the reference to the button to submit the form.
          currentPage = submitButton.click();
    
          // Get the div containing the filters
          HtmlElement filterDiv = currentPage.getElementById("search-lego-refinements");
    
          // Select the first link from the filter block (Upload date)
          HtmlAnchor sortByDateLink = ((List<HtmlAnchor>) filterDiv.getByXPath("//ul/li/a")).get(0);
    
          // Click the 'Upload date' link
          currentPage = sortByDateLink.click();
    
          System.out.println(currentPage.asText());
       }
    }
    

    You could just browse the correct query URL as well (http://www.youtube.com/results?search_type=videos&search_query=nyan+cat&search_sort=video_date_uploaded).

    But then you would have to encode your search parameter(s) (replace spaces with + for example).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package net.learn2develop.PopularAttractions; import java.io.IOException; import java.util.List; import java.util.Locale; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Authenticator; import java.net.MalformedURLException; import java.net.PasswordAuthentication;
Here is my code realising the connection. import java.io.FileNotFoundException; import java.io.IOException; import java.net.MalformedURLException; import
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
I'm confused by the following code: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class
What's wrong with this code import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; /** * *
latitude and longitude change in android and map not shown package net.learn2develop.GoogleMaps; import java.io.IOException;
import java.util.Collection; import example.Event; public interface Query { public boolean hasMore (); public Collection<Event>
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.