在home depot网站上,选择首选商店后,当您单击某个产品时,您可以查看该商店是否提供该产品(在web浏览器中)。
我正在尝试使用htmlunit从家得宝网站获取产品的库存状态。但是,当我使用htmlunit加载产品页面时,它不知道我首先设置的首选存储。
@Test
public void htmlUnitSO() throws Exception {
try (WebClient webClient = new WebClient(BrowserVersion.CHROME)) {
// Enable cookies so session is maintained between requests
CookieManager cookieMan = new CookieManager();
cookieMan = webClient.getCookieManager();
cookieMan.setCookiesEnabled(true);
// Start at the Store Finder page, passing in 3015 (the O'Fallon store) so it is at the top of the
// returned list
HtmlPage storeFinderPage = webClient.getPage("https://www.homedepot.com/l/search/3015/full/");
// Get the <a> for the O'Fallon store and click it. This is like clicking "Shop this store" in the browser
HtmlAnchor shopThisStoreAnchor = storeFinderPage
.querySelector("div#SFControl")
.querySelector("div.sfstorelinks")
.querySelector("a");
shopThisStoreAnchor.click();
// Now try to look up a product and see its inventory at the O'Fallon store
String productUrl = "https://www.homedepot.com/p/DEWALT-22-oz-Steel-Checkered-Face-Hammer-DWHT51064/205849047";
HtmlPage productPage = webClient.getPage(productUrl);
// Print out the page to view the HTML, perform a search for "Choose A Store"
// The search does find a hit, meaning the page is unaware that O'Fallon was selected already
System.out.println(productPage.asXml());
}
}
暂无答案!
目前还没有任何答案,快来回答吧!