Java如何使用动态生成的名称操作html文本元素? [英] Java how to manipulate a html text element with dynamic generated name?

查看:81
本文介绍了Java如何使用动态生成的名称操作html文本元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在网站上的input type="text"标签中操作或设置文本.但是此输入在每次加载时都有一个动态生成的名称.

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.

如何在Java中获取和操作此元素?

How to get and manipulate this element in java?

这是一个例子

<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;">

每个新加载的网站上都以"VHqnx63SwDau2nuNOOFRM2MCJ5sJawbpHv"为名称.

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

我没有找到有关此问题的真正有用的帖子.

I found no really helpful posts to this problem.

我应该使用jsoup之类的东西吗?

Should I maybe use something like jsoup?

推荐答案

是的,您绝对应该使用Jsoup.

Yes, you should definitely use Jsoup.

使用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());

这会从HTML中获取第一个输入元素,它是form元素中centre元素的子元素.

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

哪些印刷品:

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

Jsoup还提供了许多其他很酷的东西,例如直接获取或设置属性,还有更多.因此,您可以这样做:

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);

哪个会打印:

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

将值设置为其他".

这篇关于Java如何使用动态生成的名称操作html文本元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆