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.
Related
There are 2 classes with the same name
<div class="website text:middle"> A</div>
<div class="website text:middle"> 1</div>
How to get A and 1? I tried using getElementById with :eq(0) and it gives out null
Method getElementById queries for elements with a specified id, not class; I'm not sure what you were trying to query with :eq(0) either.
Try:
// String html = ...
Document doc = Jsoup.parse(html);
List<String> result = doc.getElementsByClass("text:middle").eachText();
// result = ["A", "1"]
EDIT
You can query for elements that match multiple classes! See Jsoup select div having multiple classes.
However, a colon (:) is a special character in css and needs to be escaped when it appears as part of a class name in a selector query. I don't think that jsoup currently supports this and simply treats everything after a colon as a pseudo-class.
To add to Janez's correct answer - while jsoup's CSS selector (currently) doesn't support escaping a : character in the class name, there are other ways to get it to work if you want to use the select() method instead of getElementsByXXX -- e.g. if you want to combine selectors in one call:
Elements divs = doc.select("div[class=website text:middle]");
That will find div elements with the literal attribute class="website text:middle". Example.
Or:
Elements divs = doc.select("div[class~=text:middle]");
That finds elements with the class attribute that matches the regex /text:middle/. Example
For the presented data though, I think think the getElementsByClass() DOM method is the way to go and the most general. I just wanted to show a couple alternatives for other cases.
document.querySelectorAll(".website")[0] // 0 is child index
you should use querySelector it is fully supported by every browser
check this for support details support
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.
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
I need to get value of attribute href of a tag.
I know using a.attr("href") I can get href attribute value.
But I want to know is there any other way to get href attribute as like in jTidy
(using syntax like //a/#href) for Jsoup.
Means can I use some selector syntax to get attribute directly ?
Thanks.
No, you cant retrieve the attribute value by a single selector. Its purpose is to select elements by various criteria.
But you can select only those elements which have the attribute and then ask it's value.
Element withAttr = doc.select("a[href]").first();
String attrAvlue = withAttr.attr("href");
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]