Reputation: 82
i'm trying to get a specific div by it's class. The class actually contains multiple classes seperated with spaces, but: the last class is seperated by to spaces!
Ex: class=test[SPACE]test[SPACE]test[SPACE][SPACE]test
full:
listing[SPACE]category_templates[SPACE]clearfix[SPACE]shelfListing[SPACE][SPACE]multiSaveListing
Now how did i go on about doing that?
Did not work (No Error thrown):
Elements divItemContainer = doc.select("div[class=listing category_templates clearfix shelfListing multiSaveListing]");
for (Element div : divItemContainer) {
Toast.makeText(ApplicationContextProvider.getContext(), "Got Div: ", Toast.LENGTH_SHORT).show();
}
Did not work (Thrown Error: String cannot contain whitespaces):
Elements divItemContainer = doc.select("div.listing.category_templates.clearfix.shelfListing..multiSaveListing");
for (Element div : divItemContainer) {
Toast.makeText(ApplicationContextProvider.getContext(), "Got Div: ", Toast.LENGTH_SHORT).show();
}
Did not work (No Error):
Elements divItemContainer = doc.select("div.listing.category_templates.clearfix.shelfListing.multiSaveListing");
for (Element div : divItemContainer) {
Toast.makeText(ApplicationContextProvider.getContext(), "Got Div: ", Toast.LENGTH_SHORT).show();
}
PS: The Toast is meant to purposly crash the App! It does nothing but kill and that's supposed to happen (at least at the moment)
<div class="listing category_templates clearfix shelfListing multiSaveListing"><div id="yousaveImage"></div><div class="multisave" id="multiSaveId"><a class="linksave" href="/promotion/2-for-250/ls85559"><span class="view-all">View all</span><span class="offer-2for3">2 for</span><span><span class="poundSign"></span><span class="ping-offer-finalValue">£2.50</span><span class="ping-offer-finalValue-1" style="display:none"></span><span class="pencep" style="display:none">p</span></span></a></div><div class="container"><div class="slider category_templates"><input id="itemId" value="1000000476716" type="hidden"><input id="maxQtyId" value="24.0" type="hidden"><div class="product active"><div class="slider"><div class="information active"><div class="imgContainer"><a href="/product/no-added-sugar/asda-chosen-by-you-orange-pineapple-double-strength-squash/1000000476716"><img class="" src="http://ui2.assets-asda.com:80/g/v5/501/375/5051413501375_130_IDShot_4.jpeg" data-original="http://ui2.assets-asda.com:80/g/v5/501/375/5051413501375_130_IDShot_4.jpeg" alt="ASDA Chosen By You Orange & Pineapple Double Strength Squash 2 FOR £2.50" title="" onerror="loadNoImage(this)"></a><a href="#" id="addToShoppingList" class="shoppinglist-icon icon-addto" title="Add to shopping list" aria-haspopup="true" aria-expanded="false"><span class="accessible"> Add to shopping list</span></a></div><p class="bundle-contains" style="display:none;"> Contains <span>0</span> <span>items</span></p><p class="subTitle">1.5LT</p></div></div><div class="product-content"><span class="bundle-banner" style="display:none;"> Bundle </span><span class="promoBanner"></span><span class="primaryBanner" style="display:none;"><a href="/promotion/2-for-250/ls85559" title="2 FOR £2.50">2 FOR £2.50</a></span><span class="title" id="productTitle"><a role="presentation" aria-hidden="true" tabindex="-1" href="/product/no-added-sugar/asda-chosen-by-you-orange-pineapple-double-strength-squash/1000000476716" title="ASDA Chosen By You Orange & Pineapple Double Strength Squash"><span>ASDA Chosen By You Orange & Pineapple Double Strength Squash</span></a></span><div class="product-type-icons" style="visibility:visible"><i data-contentid="" data-similarproducts="true" data-title="Suitable for Vegetarians" data-name="Suitable for Vegetarians" title="Vegetarian" class="type-icon icon-suitable-for-vegetarians" data-infoiconid="1215398078196" data-id="2854136"><a href="#" class="icons">Vegetarian</a></i></div><div class="rating-static rating-50"><span class="star star1"></span><span class="star star2"></span><span class="star star3"></span><span class="star star4"></span><span class="star star5"></span></div><div class="prod-limit-Mask"></div><div class="quantity-info-Mask"><span class="qLimit-toolTip"></span><a href="#" class="undoLimit-close" role="alertdialog" aria-labelledby="quantityLimitText"> Close </a><div class="qLimit-popUp"><p id="quantityLimitText"><span class="qLimit-Sorry">Sorry...</span>You can't add more than <span class="max-qty-val">24</span> per order</p></div></div><div id="cartBground" class="addedbg"><div class="price-cart-block"><div class="price-wrap category_templates"><span class="price"><span>£1.40</span></span><span class="priceInformation"> (9.3p/100ml) </span></div><a href="#" onclick="return false;" class="btn btn-primary addItemToTrolley fav-test-item " data-skuid="1000000476716">Add</a><a href="/product/no-added-sugar/asda-chosen-by-you-orange-pineapple-double-strength-squash/1000000476716" class="view-bundle btn btn-primary fav-bundle-test-item" style="display:none;" data-skuid="1000000476716">View bundle</a><div class="quantityOptions clearfix"><a href="#" onclick="return false;" id="minusButtonId" onselectstart="return false;" aria-label="Quantity Decrease" class="btn btn-primary button-quantity quantityDecrease"><span>–</span></a><input aria-label="Quantity in your trolley" value="1" name="quantityInTrolley" class="prd-txt" maxlength="5" type="number"><a href="#" onclick="return false;" id="plusButtonId" onselectstart="return false;" aria-label="Quantity Increase" class="btn btn-primary button-quantity quantityIncrease"><span>+</span></a><a href="#" onclick="return false;" class="btn btn-primary add-small" style="display:none;" id="applyItemToTrolley">Add</a><div id="qtySelect" class="qty-wrapper" style="display: none;"><div class="qty-select"><span class="qty-value" tabindex="0" title="Quantity">Q<span class="accessible">uanti</span>ty</span><span class="qty-select-icon"></span></div><ul class="qty-list" style="display:none"><li class="qtyAccessible"><span title="Quantity" data-salesunit="Qty">Q<span class="accessible">uanti</span>ty</span></li><li class="kgAccessible"><span title="Kilogram" data-salesunit="kg">k<span class="accessible">ilo</span>g<span class="accessible">ram</span></span></li></ul></div><p id="inTrolleyId">in your trolley</p></div></div><div id="itemAjaxLoader" class="ajaxLoader 1000000476716" style="display:none;"><img src="//ui3.assets-asda.com/theme/img/common/loader.svg" style="width: 32px;" onerror="this.src=//ui3.assets-asda.com/theme/img/common/ajax-loader.gif; this.onerror=null;"></div><div class="unavail-item-message"> Item unavailable<span class="qLimit-toolTip"></span></div><div class="unavail-item"><span class="unavailable-image"></span><span></span></div></div></div><div class="sectionMenu"></div></div></div></div></div>
Upvotes: 0
Views: 723
Reputation: 17745
This works, but it is unsafe and no reason to use it. Moreover in order for this to work you the order of the classes and the whitespaces must be identical. You say it doesn't, but I've tested it and it does.
Elements divItemContainer = doc.select("div[class=listing category_templates clearfix shelfListing multiSaveListing]");
for (Element div : divItemContainer) {
Toast.makeText(ApplicationContextProvider.getContext(), "Got Div: ", Toast.LENGTH_SHORT).show();
}
This is the way to do it. The order of the classes doesn't matter, nor the whitespaces. You say it doesn't work, but I've tested it and it does.
Elements divItemContainer = doc.select("div.listing.category_templates.clearfix.shelfListing.multiSaveListing");
for (Element div : divItemContainer) {
Toast.makeText(ApplicationContextProvider.getContext(), "Got Div: ", Toast.LENGTH_SHORT).show();
}
For this one the error is correct.
Elements divItemContainer = doc.select("div.listing.category_templates.clearfix.shelfListing..multiSaveListing");
for (Element div : divItemContainer) {
Toast.makeText(ApplicationContextProvider.getContext(), "Got Div: ", Toast.LENGTH_SHORT).show();
}
You query goes through a validation before executed. The validation that takes place takes as a parameter every class you input. The css selector you type gets split for every .
and by typing consecutive .
you are creating empty classes.
public static void notEmpty(String string) {
if ((string == null) || (string.length() == 0))
throw new IllegalArgumentException("String must not be empty");
}
The reason it doesn't work is not your selector. Try typing the response you get from the server. When you don Document doc = Jsoup.parse()...
try printing the doc
. Does this contain the element you are searching for? I'm suspecting it doesn't.
If I'm right in that the element you are searching for is not present in the response you are getting, then you have two possibilities.
bot
and doesn't allow that or it serves you a page for mobiles, so it serves you something else from what you are seeing when navigating through the browser. If this is the case then the solution is to set a userAgentUpvotes: 2