Jsoup eq selector returns no value - java

Trying to fetch data using Jsoup 1.10.3, seems like eq selector is not working correctly.
I tried the nth-child, but it seems like its not getting the second table (table:nth-child(2)).
Is my selector correct?
html > body > table:nth-child(2) > tbody > tr:nth-child(2) > td:nth-child(2)
in the example below, trying to extract the value 232323
Here is the try it sample

There are several issues that you may be struggling with. First, I don't think that you want to use the :nth-child(an+b) selector. Here is the explanation of that selector from the jsoup docs:
:nth-child(an+b) elements that have an+b-1 siblings before it in the document tree, for any positive integer or zero value of n, and has a parent element. For values of a and b greater than zero, this effectively divides the element's children into groups of a elements (the last group taking the remainder), and selecting the bth element of each group. For example, this allows the selectors to address every other row in a table, and could be used to alternate the color of paragraph text in a cycle of four. The a and b values must be integers (positive, negative, or zero). The index of the first child of an element is 1.
I guess you want to use the :table:nth-of-type(n) selector.
Second, you only select elements with your selector, but you want to get the visible content 232323, which is only one inner node of the element you select. So what is missing is the part where you get to the content. There are several ways of doing this. I again recommend that you read the docs. Especially the cookbook is very helpful for beginners. I guess you could use something like this:
String content = element.text();
Third, with CSS selector you really do to need to go through every hierarchy level of the DOM. Since tables always contain a tbody and tr and td elements, you may do something like this:
String content = document.select("table:nth-of-type(2) tr:nth-of-type(2) td:last-of-type").text();
Note, I do not have a java compiler at hand. Please use my code with care.

Related

Trying to put a list of ids with a common phrase into an array/list in selenium java

I need to get the IDs present in the DOM that start with "starx-" into an array. After that hyphen, there is usually some combination of uppercase, lowercase, and digits - it depends upon the dropdown selected previously. But starx- is common to all ids, no matter what is selected in the previous dropdown (and there are a LOT of options so... no to creating a bunch of "if" statements based on the previous drop down)
To do so, I created this line here to match all of the id's in the DOM that start with starx-:
List<WebElement> allStarX = driver.findElements(By.cssSelector("[id^='starx-']"));
So it's good at finding them. A few print statements I threw in there show me that if there are 4 such IDs in the DOM, it will find them all. If there are 7, it will have 7 elements in that List. Unfortunately , this List doesn't contain the actual IDs. It contains:
[[ChromeDriver: chrome on WIN8_1 (f665490daee44e1039265763f67008cc)] -> css selector: [id^='starx-']]
for each id beginning with starx- that exists in the DOM. Ideally I'd get all the ID using the .getAttribute method, but that's for strings. For example, if I add it onto the end of this:
List<WebElement> allStarX = driver.findElements(By.cssSelector("[id^='starx-']")).getAttribute();
I have a type mismatch. And if I do this:
for (WebElement starx : allStarX)
{
starx.getAttribute("id");
System.out.println(starx);
}
It still prints out a bunch of this:
[[ChromeDriver: chrome on WIN8_1 (f665490daee44e1039265763f67008cc)] -> css selector: [id^='starx-']]
I kind of know why that last one doesn't work, but I don't understand why the first one doesn't.
Absolute xpaths don't seem to work because there are a lot of previous options add and remove things from the DOM.
Can anyone suggest something that might help?
As far as my understanding goes, you are trying to get the IDs which start with the text of 'starx-'. You have created a list of webelements (List<WebElement>) and while extracting the text, you are looping it by not extracting the attribute of the id, which is the issue.
You can handle it as mentioned below:
List<WebElement> allStarX = driver.findElements(By.cssSelector("[id^='starx-']"));
for (WebElement starx : allStarX)
{
System.out.println(starx.getAttribute("id"));
}
Hope this helps.

Why is XPath last() function not working as I expect?

I am using Java and Selenium to write a test. I need to get the last element inside another element, so I used last() function, but the problem is that it doesn't always bring me the last one when I apply :
//a//b[last()]
to
<a>
<l>
<b>asas</b>
</l>
<b>as</b>
</a>
to get <b>as</b> ,it brings me:
<b>asas</b>
<b>as</b>
but when I apply it to:
<a>
<b>asas</b>
<b>as</b>
</a>
it brings me:
<b>as</b>
This is a common source of XPath confusion. First the straightforward parts:
//a selects all a elements in the document.
//a//b selects all b elements in the document that are
descendants of a elements.
Normal stuff so far. Next is the tricky part:
To select the last b elements among siblings (beneath a elements):
//a//b[last()]
Here, the filtering is a part of the b selection criteria because [] has a higher precedence than //.
To select the last b element in the document (beneath a elements):
(//a//b)[last()]
Here, the last() is an index on the list of all selected b elements because () is used to override the default precedence.
I think it's easiest to understand the behaviour if you remember that "//" is an abbreviation for "/descendant-or-self::node()/", and that the step "b" is an abbreviation for "child::b". So
//b[last()]
is an abbreviation for
/descendant-or-self::node()/child::b[position()=last()]
Which means "Select every node in the document (except attributes and namespaces). For each of these nodes, form a list of the child elements named "b", and select the last element in this list".
You ask for sources of information. #kjhughes recommends reading the XPath 1.0 recommendation, and indeed, it is a lot more readable than many specs. But it can be a bit terse at times; it occasionally feels like solving a crossword puzzle. My "XSLT 2.0 Programmer's Reference" (which also includes a lot of material on XPath) was written for people who want a deep understanding of how the language works, but explained in plainer English. This particular topic is on page 627, and it's easy enough to find a pirated copy on the web if you want to see how it's covered. But I'd recommend buying a legal copy, because scrolling through 1300 pages of scanned PDF is not much fun.

How to select all children (with same tag. ex.table) except first and last with jsoup

I want to get all tags (with same tag. ex. table) in one div with id = content, except first and last. The number of tags (in this case tables) is dynamic.
You can get all of them (I assume you know how to do that, otherwise the question would be stated differently?), write to a list, let's call it tables, and then do tables.sublist(1, tables.size() - 1)
Here is the full solution using selectors
Document doc = Jsoup.parse(...) // parse from some source
Elements tables = doc.select("div#content table");
tables = tables.sublist(1, tables.size() - 1);
Excerpt from doc about selectors:
el, el, el: group multiple selectors, find unique elements that match any of the selectors; e.g. div.masthead, div.logo
:not(selector): find elements that do not match the selector
:last-child elements that are the last child of some other element.
:gt(n): find elements whose sibling index is greater than n; e.g. div p:gt(2)
I guess it's a good starting point.
More here

Jdoms annoying textnodes and addContent(index, Element) - schema solutions?

i have some already generated xmls and the application causing problems now needs to add elements to it which need to be at a specific position to be valid with to the applied schemata...
now there are two problems the first one is that i have to hardcode the positions which is not that nice but "ok".
But the much bigger one is jdom... I printed the content list and it looks like:
element1
text
element2
element4
text
element5
while the textnodes are just whitespaces and every element i add makes it even more unpredictable how many textnodes there are (because sometimes there are added some sometimes not) which are just counted as it were elements but i want to ignore them because when i add element3 at index 2 its not between element2 and element4 it comes after this annoying textnode.
Any suggestions? The best solution imho would be something that automatically puts it where it has to be according to the schema but i think thats not possible?
Thanks for advice :)
The JDOM Model of the XML is very literal... it has to be. On the other hand, JDOM offers ways to filter and process the XML in a way that should make your task easier.
In your case, you want to add Element content to the document, and all the text content is whitespace..... so, just ignore all the text content, and worry about the Element content only.
For example, if you want to insert a new element nemt before the 3rd Element, you can:
rootemt.getChildren().add(3, new Element("nemt"));
The elements are now sorted out.... what about the text...
A really simple solution is to just pretty-print the output:
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
xout.output(System.out, mydoc);
That way all the whitespace will be reformatted to make the XML 'pretty'.
EDIT - and no, there is no way with JDOM to automatically insert the element in the right place according to the schema....
Rolf

Selenium webdriver: finding all elements with similar id

I have this xpath: //*[#id="someId::button"]
Pressing it shows a dropdown list of values.
Now, I know all the elements in the list have an id like this :
//*[#id="someId--popup::popupItemINDEX"]
, where INDEX is a number from 1 to whatever the number of options are.
I also know the value which I must click.
One question would be: since I will always know the id of the button which generates the dropdown, can I get all the elements in the dropdown with a reusable method? (I need to interact with more than one dropdown)
The way I thought about it is:
get the root of the initial ID, as in:
//*[#id="someId
then add the rest : --popup::popupItem. I also need to add the index and I thought I could use a try block (in order to get though the exceptions when I give a bigger than expected index) like this:
for(int index=1;index<someBiggerThanExpectedNumber;index++){
try{
WebElement aux= driver.findElement(By.xpath(builtString+index+"\"]"));
if(aux.getText().equals(myDesiredValue))
aux.click();
}catch(Exception e){}
}
Note that I am using the webdriver api and java.
I would like to know if this would work and if there is an easier way of doing this, given the initial information I have.
EDIT: The way I suggested works, but for an easier solution, the accepted answer should be seen
As a rule of thumb, try to select more elements by one query, if possible. Searching for many elements one-by-one will get seriously slow.
If I understand your needs well, a good way to do this would be using
driver.findElement(By.id("someId::button")).click();
driver.findElement(By.xpath("//*[contains(#id, 'someId--popup::popupItem') " +
"and text()='" + myDesiredValue + "']"))
.click();
For more information about XPath, see the spec. It's surprisingly a very good read if you can skip the crap!
That finds and clicks an element with text equal to you desired value which contains "someId--popup::popupItem" in its ID.
List<WebElement> list = driver.findElements(By.xpath("//*[contains(#id, 'someId--popup::popupItem')]"));
That finds all just all elements that contain "someId--popup::popupItem" in their ID. You can then traverse the list and look for your desired element.
Did you know you can call findElement() on a WebElement to search just it's children?
- driver.findElement(By.id("someId")).findElements(By.className("clickable"))
Without a peek on the underlying HTML, I guess I can't offer the best approach, but I have some in my head.
Have you tried using JavascriptExecutor?
If you are willing to write a little JavaScript then this would be straightforward than in java (I think)
All you will need to do is have some JavaScript crawl through the DOM subtree, and return a list of DOM elements matching your criteria. WebDriver will then happily marshall this as List<WebElement> in the java world.
The Safer Method to use here is
int size=driver.findElements(By.xpath("//*[#id='someId::button']")).size();
Start using Index Now
String builtString="//*[#id='someId::button'][";
for(int index=1;index<=size();index++)
{
try
{
WebElement aux= driver.findElement(By.xpath(builtString+index+"\"]"));
if(aux.getText().equals(myDesiredValue))
aux.click();
}
catch(Exception e){}
}
Please Let me know is the above funda is working or not.

Categories