2

I want to manipulate or set text in a input type="text" tag on a website. But this input has a dynamic generated name on every load.

How to get and manipulate this element in java?

Here is an example

<form method="POST" name="sform" class="form-horizontal" role="form" style="text-align: center;">
<input type="text" name="address" class="form-control" style="position: absolute; left: -5000px">
<input type="checkbox" name="honeypot" style="position: absolute; left: -5000px">
<center>
<input type="text" name="VHqnx63SwDau2nuNOOFRM2MCJ5sJawbpHv" class="form-control" value="" 
                    placeholder="Type Text here" style="width: 448px; text-align: center;">

The name "VHqnx63SwDau2nuNOOFRM2MCJ5sJawbpHv" is on every new loading the website something else.

I found no really helpful posts to this problem.

Should I maybe use something like jsoup?

Phiter
  • 14,570
  • 14
  • 50
  • 84
HaShLo
  • 43
  • 6
  • Where would this data come from? Hot did you get this enourmous name? – Pablo Dec 25 '15 at 05:50
  • @Pablo its not my site. it is a site on the internet. I dont know how they implemented it. i want to make a fill in autonomous instead of fill in manually – HaShLo Dec 25 '15 at 17:41

1 Answers1

0

Yes, you should definitely use Jsoup.

With Jsoup, it should be as simple as this:

Document doc = Jsoup.connect(url).get();
System.out.println(doc.select("form[name=sform] > center > input").first());

This gets the first input element from the HTML which is a child of a centre element which is in a form element.

Which prints:

<input type="text" name="VHqnx63SwDau2nuNOOFRM2MCJ5sJawbpHv" class="form-control" value="" placeholder="Type Text here" style="width: 448px; text-align: center;">

Jsoup also offers lots of other cool stuff like directly getting or setting attributes, and lots more. So you could do:

Element e = doc.select("form[name=sform] > center > input").first();
e.attr("value", "Something");
System.out.println(e);

Which would print:

<input type="text" name="VHqnx63SwDau2nuNOOFRM2MCJ5sJawbpHv" class="form-control" value="Something" placeholder="Type Text here" style="width: 448px; text-align: center;">

With the value set to "Something".

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
  • Thanks @JonasCz i will give a try and get back – HaShLo Dec 25 '15 at 17:38
  • @HaShLo, Let me know if you have any problem, and I'll try to help. – Jonas Czech Dec 25 '15 at 17:45
  • Hi @JonasCz it worked very well , how to submit this form now after add an attribute in it ? – HaShLo Dec 26 '15 at 10:03
  • @HaShLo, You need to manually get the data from the form and then post it to the server, check this answer here: http://stackoverflow.com/questions/23320498/how-to-post-form-login-using-jsoup . You need to get the URL where the form should be submitted to from the HTML, and then add all the input values from the form as name / value pairs, and post it. – Jonas Czech Dec 26 '15 at 10:37
  • thanks again. I tried it, but it not work with the solution of the other post. – HaShLo Dec 26 '15 at 15:44
  • i dont know if you will notified, because of the code i made a new answer. please look down, thanks – HaShLo Dec 26 '15 at 15:55