使用HtmlUnit 选中点击后弹出来的小窗空里的某一项

图片说明

自己写的代码实现不 :
public static void main(String[] args) throws Exception {

    String baseUrl = "https://www.amazon.com/Crayola-Crayons-Assorted-Toddler-16Count/dp/B07L6VQDSC/ref=br_asw_pdt-5?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=&pf_rd_r=AVBEXB73DT8S9TDFXAEJ&pf_rd_t=36701&pf_rd_p=35b30473-3e76-4fb0-bbe5-6c819fe05b28&pf_rd_i=desktop";

    WebClient webClient = new WebClient();
    webClient.addRequestHeader("user-agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Mobile Safari/537.36");
    webClient.getOptions().setUseInsecureSSL(true);
    webClient.getCookieManager().setCookiesEnabled(true);//开启cookie管理
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setCssEnabled(false);
    webClient.setJavaScriptTimeout(5000);
    //设置不自动重定向
 //    webClient.getOptions().setRedirectEnabled(false);
    //设置出现302重定向不会报错
    //      webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.setAjaxController(new NicelyResynchronizingAjaxController());
    webClient.getOptions().setDoNotTrackEnabled(false);
    //执行请求
    HtmlPage page = webClient.getPage(baseUrl);
    webClient.waitForBackgroundJavaScript(5*1000);

    DomNodeList<DomElement> input2 = page.getElementsByTagName("input");

// //获取指点标签
HtmlSubmitInput submitInput = null;
for (DomElement domElement : input2) {
if (domElement.getAttribute("title").equals("Add to Shopping Cart")) {
submitInput = (HtmlSubmitInput) domElement;
}
}
HtmlPage page1 = submitInput.click();

    //设置下拉框数量
    HtmlSelect quantity = page1.getElementByName("quantity");
    HtmlOption option = quantity.getOption(5);
    option.click();


    //下拉框下的商品数量(还是不能改变数量)

// DomNodeList span = page1.getElementsByTagName("span");
// HtmlElement htmlElement=null;
// for (DomElement domElement : span) {
// if (domElement.getAttribute("data-a-class").equals("quantity")) {
// htmlElement= (HtmlElement) domElement;
// }
// }
// if (htmlElement!=null) {
// HtmlPage page2 = htmlElement.click();
// }
DomNodeList span1 = page1.getElementsByTagName("span");
HtmlElement htmlElement1= null;
for (DomElement domElement : span1) {
if (domElement.getAttribute("class").equals("a-button-text a-declarative")) {
htmlElement1= (HtmlElement) domElement;

        }
    }
    if (htmlElement1!=null) {
        HtmlPage page2 = htmlElement1.click();
        String contentAsString = page2.getWebResponse().getContentAsString();
        System.out.println("打"+contentAsString);
    }



}

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问