Splinter childnodes div - css-selectors

I'm using Splinter (https://splinter.readthedocs.org/en/latest/) for web testing, I successfully fetch the div element by using browser.find_by_css(), the div contains child nodes. I'm using phantomjs driver.
How can I access the child nodes of the div represented by a WebDriver object
returned by browser.find_by_css()?
Thank you.

I can't understand exactly what you need, but it may be one of those:
value = browser.find_by_css('h1')
print value.first
print value[4] (for forth child)

Related

need xpath where the next descendant that has text is returned

I am trying to write a locator where the next text descendant is returned. I wont know the text. The following xpath works:
//*[#id='myChart']//label[contains(text(),"Show:")]/following::div[4]
but I dont like the div[4] as this could easily change. The element is the first div type descendant under show that contains text. Any suggestions?
A
Considering the following clauses:
the next text descendant
I wont know the text
div[4] as this could easily change
element is the first div type descendant
To locate the element a couple of effective approaches are as follows:
Using xpath:
//*[#id='myChart']//label[contains(., "Show")]//div[text()]
Using xpath with descendant:
//*[#id='myChart']//label[contains(., "Show")]//descendant::div[text()]
Using xpath with following:
//*[#id='myChart']//label[contains(., "Show")]//following::div[text()]
I think this will work for you:
//*[#id='myChart']//label[contains(text(),"Show:")]//div[text()]
To give more confident answer we need to see the actual page / XML.
In case the desired div is a direct child of the label containing the "Show:" the above expression can be presided to
//*[#id='myChart']//label[contains(text(),"Show:")]/div[text()]

Angular JS - How to find the element in DOM having the xpath?

How can I use xpath instead of id to find the element in DOM. I know that for id I can use: $("#id")[0].
I use $("#id")[0] inside Developer mode in browser in order to get the element by id to see what methods (like .getText(), innerHTML or others) am I able to use for the element. I want to know how to do this by XPATH
Thanks in advance
On Firefox console you can find (and explore) the element like this:
$x("//button[#id='myButton']")
But if you want to call a function on the element, you have to call it like this:
$x("//button[#id='myButton']")[0].click()
because there is always an array of elements returned (provided that the element is present and the first one in the array).
Thanks for the comments. I found out the answer. It can be done with:
$x("xpath")

Selecting first element verifying a condiction using xPath in libxml

In libxml, I try to select the first element to verify a condition using xpath. If I understood correctly, "//div[contains(#id,'art')][1]" in xpath would give just one element, though I get more than one of them. I use the function getnodeset in the libxml tutorial (see here). Here is the code :
xmlXPathObjectPtr result=getnodeset(def,(xmlChar*) "//div[contains(#id,'art')][1]"); // where def is a htmlDocPtr
xmlNodeSetPtr nodeset;
if(result)
{
nodeset=result->nodesetval;
if(nodeset->nodeNr>1)
fprintf(stderr,"%i first div with id attribute *art* : %s\n",nodeset->nodeNr,nomDef);
}
Instead of
"//div[contains(#id,'art')][1]"
you want
"(//div[contains(#id,'art')])[1]"
The reason has to do with binding precedence. As you probably know, [1] is shorthand for [position() = 1]. In the variant that you were trying to use, this means "when the current node (the div element) is the first child of its parent". Clearly, there could be many such divs that are each the first child of their respective parent.
When you put parentheses around the expression //div[predicate] and append [1] to that, then you're asking the question you intended to ask: what is the first node in the nodeset selected by //div[predicate]?

Selenium CSS selector by id AND multiple classes

Using selenium for the first time here, I was wondering why:
final WebElement justAnId = findElement(By.cssSelector("#someId"));
final WebElement whatIWant = justAnId.findElement(
By.cssSelector(".aClass.andAnother input[type=text]")
);
works, but not:
final WebElement whatIWant = findElement(By.cssSelector(
"div#someId.aClass.andAnother input[type=text]"
));
Although they seem equivalent to me I get:
org.openqa.selenium.NoSuchElementException: Unable to locate element:
{"method":"css selector","selector":"div#someId.aClass.andAnother input[type=text]"}
Is this intended behaviour or a bug in Selenium? I had a quick look in the bug tracker in Selenium but I didn't see anything about that. I wanted to ask here before raising an issue that doesn't need to be. Also as far as I understand it doesn't work in IE6 but who cares. I was using firefox for this run.
Actually the two are quite different selectors.
Here is your cssSelector:
div#someId.aClass.andAnother input[type=text]
But what you really wanted to write was:
div#someId .aClass.andAnother input[type=text]
notice the space between ID and class. you need that.
findElement() finds an element in the current context, which means your first snippet of code is really finding an element that matches .aClass.andAnother input[type=text], which is contained within #someId. The element with that ID may or may not contain the two classes; WebDriver doesn't assume you're referring to the same element; it just finds the input as long as its ancestors are #someId and .aClass.andAnother.
This is completely different from div#someId.aClass.andAnother input[type=text], which finds any input[type=text] within div#someId.aClass.andAnother only (i.e. it's a div that contains both the ID and the classes).

How to easily grab an array of children from a batch node by tag

I know that I can get an array of all the children of a CCSpriteBatchNode by using its children property, but can I get an array easily of just the subset of children that share a common tag?
What I do now is:
Get the array of the children of the batch node
Make a new array for the children with the tag of interest
Iterate through the children, and if the individual child has that tag, add it to the new array
Seems kind of cumbersome so I thought there might be a way to to it easily. If you just want a single child, you can use getChildByTag I think...
That's the way to do it.
However you can (and should) initialize an array with the children that use the same tag in your class, and every time you add a child with that tag you'll also add it to the "childsWithTagX" array. Same for removing. That way you have an always up-to-date separate children array containing only nodes with a given tag.
I think I'll have to add this as a feature to the Kobold2D Roadmap. I needed that a few times already.

Resources