How to get a "select" html element by Jsoup? - java

<section class="my grid">
How can I use Jsoup to optain this element (and all sub elements)?
The following does not work (is empty):
Elements ul = doc.getElementsByClass("my grid");

Elements listGrids=new Elements
for(Element section:doc.getElementsByTag("section"))
{
if(section.absUrl("Class).equals("my grid")
listGrids.add(section);
}
I don't know why your current code doesn't work, but it could be because you have a space in your value
Niko

This answer is just for your information. This could be done even easier. Just like
Elements ul = doc.select("section.my.grid");
or for iteration as
for(Element section : doc.select("section.my.grid")){
System.out.println(section.text());
}
Explanation
Esentially you could filter tag based on class by . (DOT) selector. Refer here
For eg - el.class gives all elements with class, e.g. div.masthead selects all div tags with class masthead. So in your case you have two classes "my" and "grid" for section tag. So just filter like
Elements ul = doc.select("section.my");
or
Elements ul = doc.select("section.grid")
This will give you all section tags with a class attribute of my or grid. But in case if you have multiple combination of "my" class and you want just "my" and "grid" together do nesting.
Elements ul = doc.select("section.my.grid");

Classes are separated by a space. In your case you add 2 classes to your section ("my" and "grid").
If you want to have a readable class, use a "-" to separate them.

Related

How to choose an element from divs having the same classnames (Selenium)

I am new to Selenium which is probably why I am stuck.
Here is the screenshot showing inspect where two divs are almost identical but they both have another div inside containing different text values: Image.
Could I somehow choose the div I want by checking if getText() is equal to ...?
I found the following way but as far as I know, I would never know the order of the stream:
new ArrayList<>(chromeDriver.findElements(By.xpath("//div[#class='sign-up-row']"))).get(1).click();
Thank you guys.
Using Xpath like below:
example for //div[#class='sign-up-row'] this node
use text():
//div[#class='sign-up-row' and .//span[text()='Remember me.')]]
Or use with contains:
//span[contains(text(), 'Remember')]/ancestor::div[#class='sign-up-row']
Use the dot . can find inner element:
//div[#class='sign-up-row' and contains(., 'Remember')]
You can create an xpath using the text present in the tag within the div tag. This would return the specific element you are interested in.
Example for the xpath:
//div/div[text()='textforcomparison']
You can follow below approach.
List<WebElement> lst = driver.findElements(By.xpath("//div[#class='sign-up-row']"));
for (int i = 0; i < lst.size(); i++) {
if(lst.get(i).getText().equals("Text to Be Compared")) {
lst.get(i).click();
//If you want to break the foe loop after the match
break;
}
}

Clicking an element having same class name as others

I am trying to locate and click an element which is having same className as other elements. I am unable to differentiate that element from other to click that element. Here is the HTML code of that element:
<a href="/category/men/N-fh7rea" class="accord-header">
Men
</a>
In this code, classname is same as others elements and text "Men" is also same. So made an Xpath of this:
//a[#class='accord-header' AND contains(text(),'Men') ]
Tweak the xpath a bit and use :
//a[#class='accord-header' and #href='/category/men/N-fh7rea']
You can get more granular and use :
//a[#class='accord-header' and #href='/category/men/N-fh7rea' and contains(.,'Men')]
You can alos use :
//a[#class='accord-header' and #href='/category/men/N-fh7rea'][normalize-space()='Men']
if you can't find any difference you can always count whith one, from the same objects interest you. If its for example 3rd element, save all of them as a list using findElements, and then get third element from it.
List<WebElement> elems = driver.findElements(By.xpath("//a[#class='accord-header' and #href='/category/men/N-fh7rea' and contains(.,'Men')]"));
WebElement elementThatYouLookedFor = elems.get(2);
If You need to click all of elements of this kind just use foreach loop:
List<WebElement> elems = driver.findElements(By.xpath("//a[#class='accord-header' and #href='/category/men/N-fh7rea' and contains(.,'Men')]"));
for(WebElement we : elems){
we.click(); //or any other operation
}

How to find an element present in data-hook through selenium

I have the HTML code as:
<a class="a-size-base a-link-normal review-title a-color-base a-text-bold" data-hook="review-title" href="/gp/customer-reviews/R252N8IFRXV8TW/ref=cm_cr_getr_d_rvw_ttl?ie=UTF8&ASIN=B071NZZHF9">Security concern</a>
and i have to search the element by data-hook as the class name is common for other attributes also and href and link-text are dynamic as it is changing for the next element as there are multiple elements that i need to fetch.
Can i search it by data-hook. If yes could someone help me with it!
Sure you can.
CSS:
driver.findElement(By.cssSelector("a[data-hook='review-title']"));
Xpath:
driver.findElement(By.xpath("//a[#data-hook='review-title']"));
You can also use the below code if multiple data-hook item you need.
List<WebElement> list = driver.findElements(By.className("review-title"));
for(WebElement elem : list){
System.out.println(elem.getAttribute("data-hook"));
}
As per the HTML you have shared to find the element through data-hook attribute you can use the following line of code :
driver.findElement(By.xpath("//a[#data-hook='review-title']"));
Though you mentioned class name is common it will be a good practice to keep the class name attribute in the xpath as follows :
driver.findElement(By.xpath("//a[#class='a-size-base a-link-normal review-title a-color-base a-text-bold' and #data-hook='review-title']"));
Further, though you mentioned href is changing, I will suggest to use the combination of all the three attributes to uniquely identify the element as :
driver.findElement(By.xpath("//a[#class='a-size-base a-link-normal review-title a-color-base a-text-bold' and contains(#href,'/gp/customer-reviews/') and #data-hook='review-title']"));

Jsoup Grab embedded tags

I am using Jsoup and was wondering how do you get embedded tags? I can get the section tag but I am not sure how to get the div tag inside as I have a list of elements. My question is how do I fetch a div tag inside a section tag?
this will work surely
Elements elements = doc.select("section.page-content-full div.content");
Just use the query selector syntax :
Elements elems = doc.select("section.main-page-content-full>div.content");
If you want just the first element use the following :
Elements elems = doc.select("section.main-page-content-full>div.content").first();

Get severals class same name with JSOUP

Is there a way to get HTML from severals class with same name with the plugin JSoup of Java ?
For example:
<div class="div_idalgo_content_result_date_match_local">
blablabla
</div>
<div class="div_idalgo_content_result_date_match_local">
123456789
</div>
I'd like get blablabla in one String and 123456789 in another.
I wish my question is understandable.
This can be done in several different ways.
If you want to select the div's with the class name above, you can simply use the following:
Elements div = doc.select("div.div_idalgo_content_result_date_match_local");
This will give you a collection of Element that you can iterate over.
If you after that would like to select perhaps only the first one, you can use the :eq(0)-parameter, or the first()-parameter.
Element firstDiv = div.first();
OR
Elements div = doc.select("div.div_idalgo_content_result_date_match_local:eq(0)");
Note that the second method you are selecting from the document, while in the first method you select from the collection of Element's. You can of course also change the value of the :eq(0) to something else that matches your element. There are many useful selectors that you can use that I have included a link to in the end of the answer.
The following code will split your div's into two:
Elements div = doc.select("div.div_idalgo_content_result_date_match_local");
Element firstDiv = div.first();
Element secondDiv = div.get(1);
System.out.println("This is the first div: " + firstDiv.text());
System.out.println("This is the second div: " + secondDiv.text());
JSoup Cookbook - Selector syntax

Categories

Resources