Why I must search explicitly relative elements in Selenium by xpath? - java

I'm trying to look for elements by xpath with Selenium's WebDriver:
WebElement element1 = driver.findElement(By.id("someID"));
List<WebElement> xPathElements = element1.findElements((By.xpath("//span[#class='someClass']")));
With this code, I'm getting all the elements with class='someClass' in the DOM.
Only when I add "." at the beginning of the xpath string I get all the elements with class='someClass' that are under element1
element1.findElements((By.xpath(".//span[#class='someClass']")));
What's the sense here? I called findElements from element1 so by default it should search for elements that are under element1, Why I must add the "."?

It has got nothing to do with Selenium, it is the way xpath works.
If you have something like //elem xpath will located anywhere in the document. But if you want to search for an element relative to another element or rather a descendant then you have to use a '.' or a dot like .//elem.

. - select current node
// - Selects nodes in the document from the current node that match the selection no matter where they are. As current node not specified, will search everywhere.
So .// means search everywhere inside current node.
In your case:
//span[#class='someClass'] is //span[#class='someClass']
.//span[#class='someClass'] is element1//span[#class='someClass']
See - xpath syntax

Related

Too many results when finding within element with Selenium WebDriver

I did the following search
parts.get(i).findElements(By.xpath("//li[starts-with(#class, '_lessons--row-')]"))
and it returned dozens of results, while I see in Developer Tools, that there are no more than 3 of them.
parts.get(i) returns single WebElement.
Looks like it searches not children of a given element, but over entire page. Can double slash cause this? What double slash means in XPath?
Your xpath is faulty here.
"//li[starts-with(#class, '_lessons--row-')]"
// searches from root level, to search from node preappend .:
".//li[starts-with(#class, '_lessons--row-')]"
Try your xpath with .// , normally you should start xpath with "." to stop finding elements from root.
.//li[starts-with(#class, '_lessons--row-')]
// match relative data. which starts at the document root. In your case you are trying to locate using
//li[starts-with(#class, '_lessons--row-')]
So it will return all the match in your html. If you want to locate some specific portion of element with class have start text_lessons--row- . You have to make your xpath more specific.
e.g
//div[#id='someid']//li[starts-with(#class, '_lessons--row-')]
I had a similar case, but . before // didn't help me. Just added findElements(By.xpath("your_xpath")).stream().filter(WebElement::isDisplayed).toList() as a workaround.

Getting WebElement by Text using XPath in Selenium test

I would like to find any WebElement based on text using XPath.
WebElement that I am interested to find,
Its HTML,
Basically my WebElement that I am trying to retrieve by Text contains an input element.
I currently use,
driver.findElement(By.xpath("//*[normalize-space(text()) = 'Own Hotel']"));
which does not find the WebElement above, but it usually works to retrieve all other web elements.
Even,
By.xpath("//*[contains(text(),'Own Hotel')]")
did not give me any results. Although I am interested in exact text match.
I am looking for a way to find web element by text immaterial of the elements that are present inside the web element. If text matches, it should return the WebElement.
Thanks!
It seems text is wrapped inside a label and not input. Try this
driver.findElement(By.xpath(".//label[text()[normalize-space() = 'Own Hotel']]"));
There is nice explanation about this xpath pattern here
In the HTML below:
The innerText Own Hotel within the <input> node contains a lot of white-space characters in the beginning as well at the end. Due to the presence of these leading and trailing white-space characters you can't use the location path text() as:
text() selects all text node children of the context node
As an alternative, you need to use the String Function string normalize-space(string?) as follows:
driver.findElement(By.xpath("//*[normalize-space()='Own Hotel']"));
However, it would a better idea to make your search a bit more granular adding the tagName and preferably an unque attribute as follows:
Using tagName and normalize-space():
driver.findElement(By.xpath("//input[normalize-space()='Own Hotel']"));
Using tagName, and normalize-space():
driver.findElement(By.xpath("//input[#name='ownHotel' and normalize-space()='Own Hotel']"));
References
you can find a couple of relevant discussions using normalize-space() in:
How to click on a link with trailing white-space characters on a web page using Selenium?
How to locate and click the element when the innerText contains leading and trailing white-space characters using Selenium and Python
How to click on the button when the textContext contains leading and trailing white-space characters using Selenium and Python

How to getText() for XPath with attribute 'display:none'

I would like to use getText() for one XPath, need text what is there.
//span(contains(#style,'display:none'))
XPath is working tested in firebug, I've tried getText, getAttribute, so far no luck
It's a little hard to say without the exact HTML, which you have not specified in your question...
To begin with, you need to change this:
"//span(contains(#style,'display:none'))"
To this:
"//span[contains(#style,'display:none')]"
UPDATE:
Alternatively, since the span element is not visible, you might be able to do it with:
String innerHTML = elem.getAttribute("innerHTML");
Where elem is the parent node of the span element.
Then, in order to get the actual text, you will need to parse the innerHTML string.
Because the element is invisible (it has display:none), Selenium cannot natively interact with it. You need cast your driver to JavascriptExecutor, then execute the following javascript:
$x("//span(contains(#style,'display:none'))")[0].text
The [0] returns the 1st element returned by the xpath.
This will return the inner text of the element.

How do I change part of a org.w3c.dom.Node using an xpath expression?

I'm using Java 6. Given a org.w3c.dom.Node, how do change the contents of one of its child elements (or potentially the node itself), given an xpath String expression representing one of those elements? Note by "contents", I'm always referring to text. If the child element represented by the path expression contained other child elements, those should go away and replaced with the text I want to substitute.
Thanks, - Dave
First, you find the element that the XPath expression points to (using the simple XPathAPI)
// `node` is your node
// `xpathExpr` is a String with the XPath expression
// `elem` is is element pointed by the XPath expression
Node elem = XPathAPI.selectSingleNode(node, xpathExpr);
then you use Node#setTextContent (javadoc):
On setting, any possible children this node may have are removed and, if it the new string is not empty or null, replaced by a single Text node containing the string this attribute is set to.
elem.setTextContent("This is the new content. Old content, go away!");

XPath to find element based on another XPath element

I have an Java AST and I try to find a variable inside it via XPath.
Lets say the variable is called 'foobar' I could use
//VariableDeclarator/VariableDeclaratorId[#Image='foobar']
but what if I dont know the text 'foobar', but want to read it from another element
//VariableDeclarator/VariableDeclaratorId[#Image=//SynchronizedStatement/Expression/PrimaryExpression/PrimaryPrefix/Name]
the 'Name' node has the information 'foobar' in #Image, but PrimaryPrefix/Name[#Image] does not work.
How must I rewrite the condition //SynchronizedStatement/Expression/PrimaryExpression/PrimaryPrefix/Name that it is the same as #Image='foobar' ?
Thanks
Try this XPath:-
//VariableDeclarator/VariableDeclaratorId[#Image=//SynchronizedStatement/Expression/PrimaryExpression/PrimaryPrefix/Name/#Image]

Categories