I want to select only the atomic values inside a node. For example, the "here" text in the following:
here
When I use Xpath in Java, it returns some sort of object/array, such as
[DomNode[here]]
I just want the text only.
Is this possible, and how? Thanks!
You can use the text() node test. For example, if you want to select the text of all anchors with an href attribute you could do the following XPath query:
//*/a/#href/../text()
Use:
//a[#href='']/text()
This selects all text nodes that are children of any a element in the document, that has an empty href attribute.
Related
I'm trying to select partially an element with xpath in my selenium code.
My xpath is
//iron-pages[#id='pages']//span[.='s8718216']
what I want is to select any element starting with s, the element just after span.
I tried this:
//iron-pages[starts-with(span[.='s'])
It doesn't work for me.
Can someone help me.
I think this xpath should work //iron-pages[starts-with(text(),'s')]
Or second try:
//iron-pages[starts-with(.,'s')] <- . instead of text() checks element for more properties. Not only text.
There are many properties that might contain text like innerText, innerHTML etc.
EDIT:
I just read your question again. You want to select element right after span so:
//iron-pages[#id='pages']//span[starts-with(text(),'s')] <- it will select span elements starting with text s.
If you want child elements you can use
//iron-pages[#id='pages']//span//*[starts-with(text(),'s')]
Your xpath should be
//iron-pages[starts-with(span[.='s'])//following-sibling::i[1]
it will get the next element that start with span with text s
In Selenium we are able to select an entire page (by stimulating the keys Ctrl + a) using:
WebElement element = driver.findElement(By.xpath(XPath));
element.sendKeys(Keys.chord(Keys.CONTROL, "a"));
This causes Selenium to select the entire page.
I'm trying to select only the contents of a specific web element. For example, if my XPath locates the title of a web page I want to be able to select just the title and not the entire page.
How can this be done?
Note: My goal is to combine this with: Actions(driver).moveByOffset(location.x, location.y);. First, I want to move the cursor/mouse to a element with the given coordinates (using the method moveByOffset()) and then I want to select the contents of that element (but not the entire page)..
Set the focus to the element first, assuming that it can receive focus:
element.sendKeys("");
then the second sendKeys should send the characters to that element specifically.
I have a really simple ask, i have a div in a html page that i can acces by his XPath which is : //div[2]/div/div[2]/div[2]/div[2].
I want a XPath that would give all the child of this div but i can't find it.
I need to get the element with the findElements method of Selenium, but what i've tested does not work:
My HTML code looks like this :
<div>
<input/>
<span/>
<p></p>
</div>
And the XPath I want to use like this :
//div[2]/div/div[2]/div[2]/div[2]/child
And my Java Selenium script like this :
List<WebElement> listElement = driver.findElements(By.xpath(xpath));
for(WebElement element : listElement) {
System.out.println(element.getAttribute("id"));
}
What XPath should I put to get the child of the div?
EDIT 1: I did use the * and it's working, but when i count the number of element it prints me 6. Does the * consider the children of his children as his own children ??
If the div in the HTML fragment in your question is located at
//div[2]/div/div[2]/div[2]/div[2]
then the input child element would be here:
//div[2]/div/div[2]/div[2]/div[2]/input
and all of the children elements would be here:
//div[2]/div/div[2]/div[2]/div[2]/*
Update:
EDIT 1: I did use the * and it's working, but when i count the number
of element it prints me 6. Does the * consider the children of his
children as his own children ??
No, div/* selects only the immediate children elements of the parent div.
If you're being surprised by a greater number of children than expected, it may be that the base XPath is selecting multiple elements, and you're then selecting the children of more than just the targeted div element.
Update 2:
If you cannot post a MCVE, and you're still puzzled about the number of children returned by,
//div[2]/div/div[2]/div[2]/div[2]/*
try challenging yourself on the XPath you've provided as the base:
//div[2]/div/div[2]/div[2]/div[2]/
First, try
//div[2]
Does it really select the single div as you expect?
Then try
//div[2]/div
Again, see if this one really selects the single div you expect.
Continue in this manner until you get to a place where the reality of the selected elements deviates from your expectations. There your answer will be obvious, or your have a more specific question to ask.
Without seeing your XML/HTML, this is about as good as the advice can get.
List<WebElement> childs = driver.findElements(By.xpath("//div[2]/div/div[2]/div[2]/div[2]/*"));
In that case selenium searchs for all child elements from path div[2]/div/div[2]/div[2]/div[2]/ and set them in list collection as WebElement object.
Well...I think I still haven't figured out how xpath works. I am trying to select the below element so that I can click on the second page. But what I am trying does not work. What is wrong here? I am selecting the a tag and then trying to find the one which contains ref whose value is next.
element = driver.findElement(By.xpath("//a[contains(#ref,'next')]"));
I probably see a typo there. The attribute you are trying to use is rel which has value next
So the correct xpath will be
//a[contains(#rel,'next')]
I want to get a list of values from an HTML document. I am using HTMLUnit.
There are many span elements with the class topic. I want to extract the content within the span tags:
<span class="topic">
Lean Startup
</span>
My code looks like this:
List<?> topics = (List)page.getByXPath("//span[#class='topic']/text()");
However whenever I try to iterate over the list I get a NoSuchElementException. Can anyone see an obvious mistake? Also links to good tutorials would be appreciated.
If you know you'll always have an <a> then just add it to the XPath and then get the text() from the a.
If you don't really know if you always will have an a in there then I'd recommend to use the .asText() method that all HtmlElement and their descendants have.
So first get each of the spans:
List<?> topics = (List)page.getByXPath("//span[#class='topic']");
And then, in the loop, get the text inside each of the spans:
topic.asText();
text() will only extract the text from that element, and that example you've given has no text component, only a child element.
Try this instead:
List<?> topics = (List)page.getByXPath("//span[#class='topic']");