Does jsoup support xpath? - xpath

There's some work in progress related to adding xpath support to jsoup https://github.com/jhy/jsoup/pull/80.
Is it working?
How can I use it?

JSoup doesn't support XPath yet, but you may try XSoup - "Jsoup with XPath".
Here's an example quoted from the projects Github site (link):
#Test
public void testSelect() {
String html = "<html><div><a href='https://github.com'>github.com</a></div>" +
"<table><tr><td>a</td><td>b</td></tr></table></html>";
Document document = Jsoup.parse(html);
String result = Xsoup.compile("//a/#href").evaluate(document).get();
Assert.assertEquals("https://github.com", result);
List<String> list = Xsoup.compile("//tr/td/text()").evaluate(document).list();
Assert.assertEquals("a", list.get(0));
Assert.assertEquals("b", list.get(1));
}
There you'll also find a list of features and expressions of XPath that are supported by XSoup.

Not yet,but the project JsoupXpath has make it.For example,
String html = "<html><body><script>console.log('aaaaa')</script><div class='test'>some body</div><div class='xiao'>Two</div></body></html>";
JXDocument underTest = JXDocument.create(html);
String xpath = "//div[contains(#class,'xiao')]/text()";
JXNode node = underTest.selNOne(xpath);
Assert.assertEquals("Two",node.asString());
By the way,it supports the complete W3C XPATH 1.0 standard syntax.Such as
//ul[#class='subject-list']/li[./div/div/span[#class='pl']/num()>(1000+90*(2*50))][last()][1]/div/h2/allText()
//ul[#class='subject-list']/li[not(contains(self::li/div/div/span[#class='pl']//text(),'14582'))]/div/h2//text()

HtmlUnit supports XPath. I've used this for certain projects and it works reasonably well.

Related

I want to find xpath of the Customer ID input textbox for https://netbanking.hdfcbank.com/netbanking/

unable to find xpath for the Customer Id input_textbox fot https://netbanking.hdfcbank.com/netbanking/
team give me a solution
My code:
public class test {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("netbanking.hdfcbank.com/netbanking/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);
WebElement input = driver.findElement(By.xpath(
"html/body/form/table[2]/tbody/tr/td[2]/table/tbody/‌​" +
"tr[1]/td[1]/table/tbody/tr[3]/td[2]/table/tbody/tr[2]/td[2]/span/input"));
System.out.println(input);
input.sendKeys("432323");
}
}
I get an error message similar to:
{"method":"xpath",
"selector":"html/body/form/table[2]/tbody/tr/td[2]/table/tbody‌​/tr[1]/td[1]/span[2]/a[2]"}
You have got a frames in that page. Use the code below, it works on HDFC net banking.
driver.switchTo().frame(1);
driver.findElement(By.xpath("//input[#class='input_password']")).sendKeys("abcd");
Full code:
WebDriver driver=new FirefoxDriver();
driver.get("http://www.hdfcbank.com/assets/popuppages/netbanking.htm");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//img[contains(#src,'continue_red')]")).click();
driver.switchTo().frame(1);
driver.findElement(By.xpath("//input[#class='input_password']")).sendKeys("abcd");
The XPath expression you showed selects an <a> element, whereas you said you want to select an <input> element, if I understand you correctly.
Try this XPath expression instead:
//input[#class="input_password"]
Or you could use a CSS selector, but you haven't told us what tools you're using so it's not clear whether CSS selectors are available.

Is paging broken with spring data solr when using group fields?

I currently use the spring data solr library and implement its repository interfaces, I'm trying to add functionality to one of my custom queries that uses a Solr template with a SimpleQuery. it currently uses paging which appears to be working well, however, I want to use a Group field so sibling products are only counted once, at their first occurrence. I have set the group field on the query and it works well, however, it still seems to be using the un-grouped number of documents when constructing the page attributes.
is there a known work around for this?
the query syntax provides the following parameter for this purpose, but it would seem that Spring Data Solr isn’t taking advantage of it. &group.ngroups=true should return the number of groups in the result and thus give a correct page numbering.
any other info would be appreciated.
There are actually two ways to add this parameter.
Queries are converted to the solr format using QueryParsers, so it would be possible to register a modified one.
QueryParser modifiedParser = new DefaultQueryParser() {
#Override
protected void appendGroupByFields(SolrQuery solrQuery, List<Field> fields) {
super.appendGroupByFields(solrQuery, fields);
solrQuery.set(GroupParams.GROUP_TOTAL_COUNT, true);
}
};
solrTemplate.registerQueryParser(Query.class, modifiedParser);
Using a SolrCallback would be a less intrusive option:
final Query query = //...whatever query you have.
List<DomainType> result = solrTemplate.execute(new SolrCallback<List<DomainType>>() {
#Override
public List<DomainType> doInSolr(SolrServer solrServer) throws SolrServerException, IOException {
SolrQuery solrQuery = new QueryParsers().getForClass(query.getClass()).constructSolrQuery(query);
//add missing params
solrQuery.set(GroupParams.GROUP_TOTAL_COUNT, true);
return solrTemplate.convertQueryResponseToBeans(solrServer.query(solrQuery), DomainType.class);
}
});
Please feel free to open an issue.

WebApi Help Page: don't escape HTML in XML documentation

I am using XML Documentation for my ASP.NET Web API Help Page as shown here.
I would like to know if there is a way to include html in the comments such that it will be rendered on the web page, instead of it being removed/ignored/escaped.
Specifically, I am looking for a way to create a newline, but being able to create bulleted lists, etc. would be great!
Ex/ I would like to be able to do something like this:
/// <summary>
/// CRUD operations for SalesDocument<br/>
/// This is a new line
/// </summary>
[RoutePrefix("api/SalesDocument")]
public partial class SalesDocumentController : ApiController
And have it show on the help page like this:
CRUD operations for SalesDocument
This is a new line.
Instead of this: (in this case, <br/> gets removed somehow - if I try using <p> tags, they are just escaped)
CRUD operations for SalesDocument This is a new line.
*I have already tried the <para> tag as suggested by multiple posts for tooltips, but this does not work on my help page.
Any suggestions are greatly appreciated!
In the installed XmlDocumentationProvider.cs file at Areas\HelpPage, you can look for a method called GetTagValue. Here modify the return value from node.Value.Trim() to node.InnerXml.
private static string GetTagValue(XPathNavigator parentNode, string tagName)
{
if (parentNode != null)
{
XPathNavigator node = parentNode.SelectSingleNode(tagName);
if (node != null)
{
return node.InnerXml;
}
}
return null;
}
Now open the installed file Areas\HelpPage\Views\Help\DisplayTemplates\ApiGroup.cshtml and modify the following line from:
<p>#controllerDocumentation</p>
to
<p>#Html.Raw(controllerDocumentation)</p>
#controllerDocumentation does not work for me, but changing the line to#api.Documentation works. i.e. #html.raw(api.Documentation).

org.w3c.dom.Document object in RFT

I am trying to use xpath in RFT. Searching over the net threw this code at me-
private static NodeList getNodesWithXPath(Document document, String xpathStr)
throws XPathExpressionException {
NodeList nodes = null;
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
if (xpathStr != null) {
XPathExpression expr = xpath.compile(xpathStr);
Object result = expr.evaluate(document, XPathConstants.NODESET);
nodes = (NodeList) result;
}
return nodes;
}
Now, I am new at RFT and totally at a loss about how to access the 'Document' object? Trying to typecast TestObject into this throws an exception. I could see a few examples stating something like
Document doc = parse(xmlFileLocation)
but I am not sure what this xmlFileLocation means. I have a simple web page where I need to identify the element using xpath.
P.S. - I understand there are other ways of identifying the object using atDescendant, etc, but I need to explicitly use xpath here due to some reasons.
Any help would be greatly appreciated.
Thanks!
They are different Document objects.
I think you got the code from IBM developerworks, and that is a document obtained from an XML file, thus browsable with XPATH.
RFT Document is com.rational.test.ft.object.interfaces.DocumentTestObject
while XML related one probably is org.w3c.dom.Document.
Totally different classes.
XPath is not supported by RFT and also no answers in the forums.
If you need XPath queries in your pages try something else, like Selenium.

Get By.XPath from WebElement used in #Find(how=How.XPATH, using="//a[.='Test']")

For debugging and reporting purposes, I would like to get the using-part of #Find from a WebElement is this possible? In debug I can go into the WebElement and see its By.XPATH via it's LocatorHandle, so there must be a way to get it during code-time?!
I cannot ofc use the GetAttribute, because I assume the element won't exist, otherwise no need to mention it in the report.
For visualization ;)
#FindBy(how = How.XPATH, using = "//a[.='Test']")
private static WebElement btnTest;
public static String GetOrgXpath(WebElement e)
{
return getByXPath(e); // should return "//a[.='Test']"
}
otherwise I would also accept to override WebElement and add a public string to store its xpath, if thats even possible. My Java KungFoo is weak, so dunno how to overwrite Interfaces :)
Thanks for your help

Resources