Using Java how can I lay down text to an HTML textbox that responds to a users keyup action?

74 views Asked by At

I'm building a Java application that interacts with a public website (I do not have access to the server-side).

The website's search function is a javascript overlay that dynamically searches on the fly as the user types their search criteria in (much like google when you type the first few characters) using aKeyUp listener.

I know how to use Java to post when it comes to submitting text to a form. But how can I send text to the text-box mentioned above to simulate actually typing the text on the webpage? Adding the value= attribute doesn't work. And I presume sending a POST request wouldn't work either.

The source code is simply

<input type="text" placeholder="Find a user by username or interest" class="opensans" />

The code doesn't change even after I've typed something in the box.

I've identified that this is using a javascript KeyUp Listener.

In brief, using Java how can I lay down text to an HTML textbox that responds to a users keyup action?

2

There are 2 answers

0
Dean J On BEST ANSWER

If you're trying to build a scraper for Google, you're likely breaking their terms of service; tread carefully.

That said, unless your Java program is built into a browser or browser extension, there's no "keyup/keydown" event to hook into. The remote server only sees HTTP requests. When you say "I know how to make a POST request", that's part of it.

If you go to the website you're trying to work with, and open Chrome's Dev Tools, there's a way to figure this out. Specifically, right click, "inspect element", then click the "Network" tab. Then reload the page. Clear the tab by hitting the red button. Then start typing into the text box, and watch what network requests it makes.

If you can use a Java program to replicate that series of network requests, you'll be replicating the keydown/keyup activity as the server sees it. For Google, these are XHR requests in HTTPS that get a 200 response. There's also some secret sauce baked into Google to prevent you from doing exactly this, so good luck, in general.

0
Ahmed Ashour On

Have you tried using htmlElement.type("something"); then wait a little for the JavaScript to work?