CSS selector for table of a particular class in Selenium - css

Even though I am going to use this CSS selector in Selenium, this should be generic enough.
I have a page with table of class "list" & it can occur multiple times. I want to find out each occurrence & how many rows each table has. So for this I could use table[class='list'] & will give me all the tables of that class in the page. In this example lets us say it is 3. Now I want to iterate through each of those table, so I tried table[class='list']:nth-child(1) for the first occurrence & table[class='list']:nth-child(2) for second occurrence & so on. I thought that table[class='list']:nth-child(1) would give me the first occurrence but I cannot use the nth-child(n) notation.
If I use table[class='list']:nth-child(odd), I do get all the odd numbered table, but I cannot specifically target a specific table by saying table[class='list']:nth-child(3). It gives me no result back. What am I doing wrong?
BTW, I am using "FireFinder" addon for Firebug to evaluate my CSS selectors on the test page.

table[class='list']:nth-child(1) will match all table elements with a class of list that are the first child of their parent. It has nothing to do with how many elements are matched or what the order of that set is, though if all the tables had the same parent (and that parent had no other children) then your method would work.
You may be able to iterate through the elements returned by table.list some other way, or somehow change your selector the exact details of which would depend on the actual structure of your page.

Perhaps XPath would suit you better?
//table[#class='list' and position()=1]
//table[#class='list' and position()=2]
...

Related

CSS/RegEx: Select elements where a certain attribute contains only numbers at certain locations

I am currently working on a web app and I need to query a data attribute that repeatedly shows up in a table.
In the table that I declare a data-test-id attribute that I can use later for testing. One example:
<td data-test-id="table-element-1-9:0-cell"> Hello world!<td>
I am querying the data attributes with a css selector for testing. The only thing that changes with each use of the data-test-id attribute are the three numbers which I will replace with X here:
/* css selector */
.select('[data-test-id="table-element-X-X:X-cell"]')
How do I create a regular expression to match all of the other characters is this attribute while ignoring the numbers denoted by X (as in the number denoted by X can be any number from 0-9)?
I tried using the following but it doesn't seem to work:
/\[data-test-id="table-element-[0-9]-[0-9]:[0-9]-cell"\]/g
Well AFAIK there is no regex-matching attribute selector in CSS. You could instead use a broader selector of
[data-test-id^="table-element-"][data-test-id$="-cell"]
This matches all elements with a data-test-id attribute that starts with table-element- and ends in -cell. Of course that might not be specific enough depending on the possible values of that attribute.
If that is the case, you could further filter the elements that are returned by above query using whatever language you call the select function from. For example using the regex you provided above.

Sitecore Xpath comparing two fields

I would like to have a query that looks at the name of its parent and then will navigate to the folder with the same sitename underneath the content folder and show the items underneath it in the multilist.
Basically the structure in the content tree looks as followed:
sitecore
content
Sitename1
(items that needs to be showed in the multilist)
Sitename2
(items that needs to be showed in the multilist)
medialibrary
Sitename1
PDF1 (with multiList with search)
Sitename2
PDF2 (with multiList with search)
Trouble starts when I would like to start comparing the "name" of the relative parent to the "name" of the child of the absolute path. In Xpath it would probably go something like it is described within: Compare attribute values using Xpath
In this case I had the following query up until now:
/sitecore/content/*[ancestor-or-self::*[##templateid='{Template Id of Sitename}']=##name and ##templateid='{Template Id of Sitename}']
This query returns Sitename1 and Sitename2.
Funny thing is that if I replace "ancestor-or-se.." or "##name" part bij "Sitename1", like so:
/sitecore/content/*['Sitename1'=##name and ##templateid='{Template Id of Sitename}']
..and..
/sitecore/content/*[./ancestor-or-self::*[##templateid='{Template Id of Sitename}']='DSW' and ##templateid='{Template Id of Sitename}']
I get the wanted result: Sitename1.
Btw I'm using the build-in xpath builder for now, before it paste the query into the "multilist with search."
Any help would be appreciated.
Edit:
I think I found out that when I start the relative query (the "./ancestor::..." part) it actually is relative to the item where I ended up with the absolute query. So I should have the following query:
./ancestor-or-self::*[##templateid='{Template Id of Sitename}' and ##name=ancestor::*[##templateid='{Template Id of root item aka "sitecore"}']//*[##templateid={Template Id of Sitename}]]
Here I get the error "Object must be of type String," which is probably because of the following part of the previous query:
##name=ancestor::*[##templateid='{Template Id of root item aka "sitecore"}']//*[##templateid={Template Id of Sitename}]
The right part of this doesn't solve to a string. So the question remains, how to extract pure the string out of a sitecore item using sitecore xpath in order to be able to make a comparison.
I figured out that Sitecore doesn't support subqueries at least for fast queries, I think same applies to normal ones (see also: "Subqueries are not supported" in here ). Which now lead me to using simple code where I perform two queries. A very simple way to do it is to inherit from IDatasource (in the sitecore.buckets.dll), you will need to write "code:{fullpath to class}, assemblyname.dll" (See also: here)

Issue is clicking web-element in a table in qtp

I have a question in UFT 12.
My scenario is:
1. Type text in a WebEdit. Say "GOOG"
2. When I type "GOOG" slowly, it lists out the symbols that start with "G". From this list out, I want to select the first element.
3. Once I click on the first element, the Google quotes are displayed.
My Issue:
UFT just types GOOG and doesn't show me the list that lays out for me to select the first element that starts with "G".
From the Object spy, I managed to find the first element as a web element.
I tried many different ways like childitem etc. Nothing seems to work. Please help me out.
I am not able to attach my screen shot here. This is something similar to Google suggest where you type and it suggests you so many options and go with the first option.
It seems like you're trying to automate auto-complete textbox. So you should not directly paste the string value in the textbox as we used to do for normal edit controls, instead you should split the string into separate characters and type one by one.
If if you do in such a way, then the AUT will list the matching options to pick. From there you can select the required option. Then you may use SendKeys method (** something like Down Arrow + Enter) to pick the selection.
Just a have attempt in this way - good luck!

operations on document tree in CSS?

I'm pretty sure this doesn't exist, but I wonder if anyone has done this kind of thing as an add-on for CSS.
Let's say we have an XML document containing the Code of Federal Regulations (CFR), and also say that the document itself cannot be edited (all we can do is use a style sheet to display it), and let's say that there is a subpart which contains multiple rules, and the XML document is roughly as follows:
<subpart>
<ruleNo>63.400</ruleNo>
<ruleTitle>Definitions</ruleTitle>
<ruleNo>63.410</ruleNo>
<ruleTitle>Applicability</ruleTitle>
...
</subpart>
Now I would like to write a stylesheet that puts the subpart content into a table, with rule numbers on the left column and rule titles on the right column. The task would be much easier if there were a way to select an adjacent ruleNo and ruleTitle as if they corresponded to a single node, and then write a rule for that imaginary node.
For example, there could be a node-defining functor => which would temporarily define as a node the part in braces. For example, Rule => subpart > {ruleNo + ruleTitle} would create a temporary node which would be the parent of any pair of siblings ruleNo + ruleTitle which were children of subpart.
CSS does not support manipulating the document tree, nor does it offer an ability to group element nodes in the manner that you describe. Additionally, the CSS2.1 table model does not offer a way to apply styles such that every ruleNo + ruleTitle pair can be formatted as an individual row: while CSS has a concept of anonymous boxes that can be created as ancestors or descendants, which also extends to the table model, it does not support generating a single anonymous box that acts as the parent row of a pair of ruleNo + ruleTitle cells at a time.
Preprocessor languages such as Sass and LESS are irrelevant as they're compiled into CSS anyway, so if it's not doable with CSS then neither is it doable with a preprocessor.
Given your current XML, it's not possible to use CSS to format it into a table with each row containing a ruleNo and a ruleTitle element. You'll want to use XSLT to transform it into an XHTML table with the corresponding rows instead.

Filtering a multivalued attribute in StringTemplate

I have a template which uses the same multivalued attribute in various places. I often find myself in a situation where I would like to filter the attribute before a template is applied to the individual values.
I can do this:
<#col:{c|<if(cond)><# c.Attribute2 #><endif>};separator=\",\"#>
but that is not what I want, because then there are separators in the output separating "skipped" entries, like:
2,4,,,6,,4,5,,
I can modify it to
<#col:{c|<if(c.Attribute1)><# c.Attribute2 #>,<endif>};separator=\"\"#>
Which is almost OK, but I get an additional separator after the last number, which sometimes does not matter (usually when the separator is whitespace), but sometimes does:
2,4,6,4,5,
I sometimes end up doing:
<#first(col):{c|<if(cond)><# c.Attribute2 #><endif>};separator=\"\"#>
<#rest(col):{c|<if(cond)>,<# c.Attribute2 #><endif>};separator=\"\"#>
But this approach fails if the first member does not satisfy the condition, then there is an extra separator in the beginning:
,2,4,6,4,5
Can someone give me a better solution?
First, let me point out that I think you are trying to do logic inside your template. Any time you hear things like "filter my list according to some condition based upon the data" it might be time to compute that filtered list in the model and then push it in. That said something like this might work where we filter the list first:
<col:{c | <if(c.cond)>c<endif>}:{c2 | <c2.c.attribute>}>
c2.c accesses the c parameter from the first application
The answer by "The ANTLR Guy" didn't help in my case and I found another workaround. See at Filter out empty strings in ST4

Resources