Get element by class in JSoup - java

I try to get all info contained in div class named : bg_block_info, but instead i get info for another div class <div class="bg_block_info pad_20"> Why i'm getting it wrong ?
Document doc = Jsoup.connect("http://www.maib.md").get();
Elements myin = doc.getElementsByClass("bg_block_info");

You can combine and chain selectors to refine your query, e.g.:
Document doc = Jsoup.connect("http://www.maib.md/").get();
Elements els = doc.getElementsByClass("bg_block_info").not(".pad_10").not(".pad_20");

That element has two classes (notice the space between bg_block_info and pad_20):
<div class="bg_block_info pad_20">
So it does have the class bg_block_info and your code is working as expected.

Elements downloadLinks = dContent.select("a[href]");
Elements pdfLinks = downloadLinks.select("a[data-format$=pdf]");
Full reference jsoup selector syntax

In your case you probably might use Element content = doc.getElementById("pollsstart"); instead Elements myin = doc.getElementsByClass("bg_block_info");.

Just use comma between bg_block_info" and "pad_20". It should be like this.
Elements myin = doc.getElementsByClass("div.bg_block_info.pad_20");

Related

How to Fetch Text from two separate div's with same class name?

I have to fetch two labels 'Text 1', 'Text 2' which belongs to same class ='xyz', which are located in two div's.Structure as shown below.
<div class='xyz'>TEXT 1</div>
<div class='xyz'>TEXT 2</div>
Can anyone please help me to solve this ?
You find elements by className and then use getText() to get the text:
List<WebElement> elements = driver.findElements(By.className("xyz"));
for(WebElement element:elements) {
System.out.println(element.getText());
}
Use FindElements method and then access to necessary div using index, e.g:
var elements = driver.FindElements(By.CssSelector((".xyz"));
//get text in first element;
elements[0].getText();
//in second
elements[1].getText(); //etc

WebDriver filtering list of elements

how to efficiently find and filter list of elements.
here is the HTML
<span class="tab-strip-text" unselectable="on">Admin</span>
<span class="tab-strip-text" unselectable="on">User</span>
<span class="tab-strip-text" unselectable="on">Reports</span>
<span class="tab-strip-text" unselectable="on">Logs</span>
currently i am using following method to find and filter and click on the element i want based on text
public static void clickTab(String tabText){
List<WebElement> tabs = driver.findElements(By.className("tab-strip-text"));
for(WebElement tab : tabs){
if(tab.getText().equals(tabText)){
tab.click();
break;
}
}
}
is there better way to find and iterate over list (to click based on text() of elements?)
thx
Use XPath with the text you are after in your locators.
//*[#class='tab-strip-text' and text()='Reports']
Then you have:
WebElement reportTab = driver.findElement(By.xpath("//*[#class='tab-strip-text' and text()='Reports']"));
reportTab.click();
Note I don't encourage you to use text in your locators if your site supports multi-languages. In that case, the best way is to add meaningful class names to your source of each element.
try this xpath
//span[contains(text(),'Reports')]
String value="text you are looking for";
public void method(String value){
driver.findElements(By.xpath( //span[contains(text(),'"+value+"')])).click();
}
I know that this post for long time ago but i guess someone will look for a solution :)
tabs.forEach(tab -> {
if(tab.getText().equals(tabText)){
tab.click();
}
}
I am Using Stream filter(). This will help you to click on element based on given text.
filter returns a stream consisting of the elements of this stream that match the given predicate.
List<WebElement> categories = driver.findElements(By.className("tab-strip-text"));
categories.stream().filter(ele->ele.getText().equalsIgnoreCase("Admin")).forEach(ele -> ele.click());

look for element and append a child to it

below i try to look for element and append a child to it ;but what the wrong with it!!??
// Document doc;
Element cust = doc.createElement("cust");
cust.appendChild(doc.createTextNode("anyname"));
org.w3c.dom.Node custmers = doc.getElementsByTagName("custmers").item(0);
custmers.appendChild(cust);
If you want to avoid navigating to the specific element, then try using xpath.
Link

Retrieving Reviews from Amazon using JSoup

I'm using JSoup to retrive reviews from a particular webpage in Amazon and what I have now is this:
Document doc = Jsoup.connect("http://www.amazon.com/Presto-06006-Kitchen-Electric-Multi-Cooker/product-reviews/B002JM202I/ref=sr_1_2_cm_cr_acr_txt?ie=UTF8&showViewpoints=1").get();
String title = doc.title();
Element reviews = doc.getElementById("productReviews");
System.out.println(reviews);
This gives me the block of html which has the reviews but I want only the text without all the tags div etc. I want to then write all this information into a file. How can I do this? Thanks!
Use text() method
System.out.println(reviews.text());
While text() will get you a bunch of text, you'll want to first use jsoup's select(...) methods to subdivide the problem into individual review elements. I'll give you the first big division, but it will be up to you to subdivide it further:
public static List<Element> getReviewList(Element reviews) {
List<Element> revList = new ArrayList<Element>();
Elements eles = reviews.select("div[style=margin-left:0.5em;]");
for (Element element : eles) {
revList.add(element);
}
return revList;
}
If you analyze each element, you should see how amazon further subdivides the information held including the title of the review, the date of the review and the body of the text it holds.

Jsoup: Optimal way of checking whether a <div> has an ID

I am able to iterate through all div elements in a document, using getElementsByTag("div").
Now I want to build a list of only div elements that have the attribute "id" (i.e. div elements with attribute "class" shouldn't be in the list).
Intuitively, I was thinking of checking something like this:
if (divElement.attr("id") != "")
add_to_list(divElement);
Is my approach correct at all?
Is there a more optimal way of testing for having the "id" attribute? (the above uses string comparison for every element in the DOM document)
You can do it like this:
Elements divsWithId = doc.select("div[id]");
for(Element element : divsWithId){
// do something
}
Reference:
JSoup > Selector Syntax
Try this:
var all_divs = document.getElementsByTagName("div");
var divs_with_id = [];
for (var i = 0; i < all_divs.length; i++)
if (all_divs[i].hasAttribute("id"))
divs_with_id.push(all_divs[i]);

Categories