There is this element which has child elements, those child elements again have child elements and so on. I would like to get all elements that are descendants of the element. Thanks.
Try this one:
(Java)
List<WebElement> childs = rootWebElement.findElements(By.xpath(".//*"));
(C#)
IReadOnlyList<IWebElement> childs = rootWebElement.FindElements(By.XPath(".//*"));
Try this one
List<WebElement> allDescendantsChilds = rootWebElement.findElements(By.xpath("//tr[#class='parent']//*"));
The above thing will gives you all descendant child elements (not only immediate child) of parent tr
Try this one:
List<WebElement> childs = rootWebElement.findElements(By.tagName(".//*"));
Related
Not a duplicate: I am not asking for the <option> within a <select>. I would like to return the <select> web elements themselves as a List. When achieved, the list should have a size of 2.
I have only found driver.findElements() that returns a List<WebElement>.
How do I return a List<Select> by ng-model="income.frequency"?
(Note: I have ngWebDriver so I can use ByAngular.model("income.frequency") as locator.)
HTML:
In order to match elements by their attribute, you can use the CSS selector:
By.cssSelector("<tag>[<attribute>='<value>']")
And in context:
List<WebElement> elements = driver.findElements(By.cssSelector("select[ng-model='income.frequency']"));
Edit: According to Selenium documentation, you can create a Select with a very straight-forward constructor. Hence, if you want a list of Select objects, just construct a new list:
List<Select> selectList = new ArrayList<>();
List<WebElement> elements = driver.findElements(By.cssSelector("select[ng-model='income.frequency']"));
for(WebElement element : elements) {
selectList.add(new Select(element);
}
below i try to look for element and append a child to it ;but what the wrong with it!!??
// Document doc;
Element cust = doc.createElement("cust");
cust.appendChild(doc.createTextNode("anyname"));
org.w3c.dom.Node custmers = doc.getElementsByTagName("custmers").item(0);
custmers.appendChild(cust);
If you want to avoid navigating to the specific element, then try using xpath.
Link
In jsoup Element.children() returns all children (descendants) of Element. But, I want the Element's first-level children (direct children).
Which method can I use?
Element.children() returns direct children only. Since you get them bound to a tree, they have children too.
If you need the direct children elements without the underlying tree structure then you need to create them as follows
public static void main(String... args) {
Document document = Jsoup
.parse("<div><ul><li>11</li><li>22</li></ul><p>ppp<span>sp</span</p></div>");
Element div = document.select("div").first();
Elements divChildren = div.children();
Elements detachedDivChildren = new Elements();
for (Element elem : divChildren) {
Element detachedChild = new Element(Tag.valueOf(elem.tagName()),
elem.baseUri(), elem.attributes().clone());
detachedDivChildren.add(detachedChild);
}
System.out.println(divChildren.size());
for (Element elem : divChildren) {
System.out.println(elem.tagName());
}
System.out.println("\ndivChildren content: \n" + divChildren);
System.out.println("\ndetachedDivChildren content: \n"
+ detachedDivChildren);
}
Output
2
ul
p
divChildren content:
<ul>
<li>11</li>
<li>22</li>
</ul>
<p>ppp<span>sp</span></p>
detachedDivChildren content:
<ul></ul>
<p></p>
This should give you the desired list of direct descendants of the parent node:
Elements firstLevelChildElements = doc.select("parent-tag > *");
OR You can also try to retrieve the parent element, get the first child node via child(int index) and then try to retrieve siblings of this child via siblingElements().
This will give you the list of first level children excluding the used child, however you'd have to add the child externally.
Elements firstLevelChildElements = doc.child(0).siblingElements();
You could always use the ELEMENT.child(index) with the index you can choose which child you want.
Here you can get the value of first-level children
Element addDetails = doc.select("div.container > div.main-content > div.clearfix > div.col_7.post-info > ul.no-bullet").first();
Elements divChildren = addDetails.children();
for (Element elem : divChildren) {
System.out.println(elem.text());
}
I have this XML code:
<root>
<node>
</first_child>
</second_child>
</third_child>
</node>
</root>
I need to take all children nodes one by one and save like three Node variable using DOM.
If I use
doc.getElementsByTagName("node");
I take this "node" with all the children, while I need only "first_child, second_child and third_child"
How to obtain this?
Element el = (Element)(doc.getElementsByTagName("node").item(0));
NodeList children = el.getChildNodes();
for (int i=0; i<children.getLength(); i++) {
System.out.println(children.item(0).getNodeValue());
}
Element el;
el = (Element) doc.getElementsByTagName("node").item(0);
el.getChildNodes();
You can get the children in this way.
var children = document.getElementById('node').getElementsByTagName('*');
I have a xml structure as follows:
<rurl modify="0" children="yes" index="8" name="R-URL">
<status>enabled</status>
<rurl-link priority="3">http</rurl-link>
<rurl-link priority="5">http://localhost:80</rurl-link>
<rurl-link priority="4">abc</rurl-link>
<rurl-link priority="3">b</rurl-link>
<rurl-link priority="2">a</rurl-link>
<rurl-link priority="1">newlinkkkkkkk</rurl-link>
</rurl>
Now, I want to remove a child node, where text is equal to http. currently I am using this code:
while(subchilditr.hasNext()){
Element subchild = (Element)subchilditr.next();
if (subchild.getText().equalsIgnoreCase(text)) {
message = subchild.getText();
update = "Success";
subchild.removeAttribute("priority");
subchild.removeContent();
}
But it is not completely removing the sub element from xml file. It leaves me with
<rurl-link/>
Any suggestions?
You'll need to do this:
List<Element> elements = new ArrayList<Element>();
while (subchilditr.hasNext()) {
Element subchild = (Element) subchilditr.next();
if (subchild.getText().equalsIgnoreCase(text)) {
elements.add(subchild);
}
}
for (Element element : elements) {
element.getParent().removeContent(element);
}
If you try to remove an element inside of the loop you'll get a ConcurrentModificationException.
If you have the parent element rurl you can remove its children using the method removeChild or removeChildren.
Use removeChild()
http://download.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/Node.html#removeChild(org.w3c.dom.Node)