selenium - extracting value from cell (td) - java

I'm writing a Java test using cellenium, in order to validate the correctness of data I'm trying to extract the values of the table cells, although all cells have different values and meaning the <td> of all cells look the same and have the same attributes like so:
<td onclick="show_data('2','2','rowDetails.php','myID','434b2410aef9e61d6237dbbe562689a9b84','644');">2</td>
the naive solution would be to extract all tag <td> and then go by index.
Is there a better way?

If your elements have no unique identifier then the common way to solve your problem is by fetching all the TD elements and loop over them. This is something you already seem to be aware of, as you're describing this as: extract all tags and then go by index.
However, your provided example does contain a unique identifier, namely the property of the onclick attribute. By using CSS selectors, don't use XPath, you can do a select based on the property (the value) of the onclick attribute. This should help you to narrow down the elements you are looking for.
For list of CSS Selectors see: http://www.w3schools.com/cssref/css_selectors.asp

Related

zk grid template "model", refers to specific element

I have grid with template "model" which takes a list. Each element of list has unique name.
How to refer to specific element by name? Because I want to set that row to be visible or not based on certain condition.
The syntax should be row_name.attribute, in this case:
rowName.visible = condition (true/false)
You can set HTML attributes on ZK elements to be able to refer to them by name but it is a bit of a "hack" because that is not the purpose of how ZK is intended to function.

How do I get the CSS selector of an element with Jsoup?

I have an element extracted from the DOM using JSOUP. I want to get the CSS selector of that element, so I can quickly find the equivalent elements on other pages with the same structure. Is this possible?
Thanks
I doubt that it's possible, because multiple selectors could be valid for your element -- eg, the trivial Selector.select("*",rootElement) would match it.
It sounds like you don't want to use the same element-extraction code (that you initially used) for all subsequent documents? If you're intent on using selectors, then perhaps try different ones until you find one that you're happy with (from either a readability or a performance perspective).

Selenium IDE - Find link within the same row of a specific text from table

I have a specific table that is within multiple tables, and does not have any unique ID or Name. I'm looking for a dynamic solution to find a specific text that will surely occur only once in the whole document, and get the WebElement of the link (text = Add) within the same row.
The text i'm looking for is in the 1st column, and the link is in the 6th column.
Basically i'm looking for a selenium alternate of nextSibling() with a twist that it has to be a link.
selenium supports XPATH which has the notions of siblings and first children etc. You can iterate but this approach makes it more robust. You can select elements too.
selenium IDE supports XPATH for finding the value or location uniquely in web page and it show notions of siblings and first children etc.
you can used Xpath of that value to find it.

JSoup Element selection by CSS rule: is there any documented ordering?

I am currently using JSoup CSS selection to get a list of some elements in an HTML document.
Though to verify the robustness of my algorithm I have to know in which order the elements do get browsed and, therefore, returned.
My concern is strictly linked to nested elements. If i do search for all the elements in a document which is like the following:
<div> Something <span style='color:red;'>special</span> for me </div>
and i run in JSoup:
Document doc = Jsoup.parse(myCode);
Elements els = doc.select("*");
in which order will those two elements be traversed, and therefore returned? I am currently looking at the documentation page for the select method, but no information is provided on the traversal order. Is there any more precise reference I can look at?
Clearly i can proceed in a trial-and-error to infer the ordering, but I would like to know if this is already known/someone has already digged into it, since I do not know the HTML structure of the documents I have to parse beforehand.
Thanks!

How to generate an HTML table in Sitebricks?

Using Sitebricks, I want to generate a table where one object backs each <td> in a table. The only examples I've seen have one object back an entire <tr> - so the HTML is consistent across each row. I would like to be able to wrap N entries in <tr>.
I don't want to have to have my page object in Sitebricks be aware of the layout of the page (and so have to add indices or structure the items as a List<List<Something>>).
Any ideas?
Edit: This is, of course, not limited to Sitebricks, but is a general question about separation of model from view using web templating systems.
Yep, you can add #Repeat on any tag. The implicit variables index and last are defined for you to do your own logic inside the repeat. You could, for example, add a CSS class if index % 2 == 0 to color even rows differently.
Here is a testcase showing how this works for non-table tags (the tags really don't matter):
https://github.com/dhanji/sitebricks/blob/master/sitebricks-acceptance-tests/src/main/resources/Repeat.html
Seems like you can put the #Repeat in front of anything. I don't think it cares about whether it's a row in a table or a column.
https://github.com/dhanji/sitebricks/blob/master/sitebricks-acceptance-tests/src/main/resources/Repeat.html
If you're trying to keep track of the index so you can emit special stuff every nth row, I don't know.

Categories