用浏览器打开相关页面,用浏览器的审查元素(或称检索)功能,观察其父节点,或者通过周围节点看看能否找到那个按钮
以某聘网搜索框按钮为例,如图,它没有id和name
但是它旁边的搜索框有name。所以,可以通过htmlpage.getElementByName("key")找到搜索框
如下图右,发现搜索框的父节点(parentNode)是:input-main float-left
然后,input-main float-left的父节点是 search-main-top
最后,发现search-main-top的lastElementChild就是要找的那个Button
上完整代码(用了jsoup和htmlunit-2.33的包):
import java.io.IOException;
import java.util.List;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
public class Ddkjg {
public static void main(String[] args) throws IOException {
WebClient webclient = new WebClient();
webclient.getOptions().setCssEnabled(false);
webclient.getOptions().setJavaScriptEnabled(false);
HtmlPage htmlpage =(HtmlPage) webclient.getPage("https://www.liepin.com");
HtmlTextInput searchInput = null;
HtmlSubmitInput submitBtn = null;
searchInput = (HtmlTextInput) htmlpage.getElementByName("key");
searchInput.setValueAttribute("java");
final HtmlPage nextPage = ((DomElement)(htmlpage.getElementByName("key").getParentNode().getParentNode())).getLastElementChild().click();
String result = nextPage.asXml();
System.out.println(result);
}
}