Not working xpath in selenium - java

The first xpath is working whereas the second not:
First:
"//*[#id='j_idt46:j_username']";
Second:
"//*[contains(#id,'username']";
Why?

To what could be figured out of the information provided, the way you are using contains is possibly inappropriate :
As mentioned by #TuringTux - //*[contains(#id,'username')] could be the possible change if the same lined goes as it is in your code.
Also a good practice to follow in //*[contains(#id,'username')] , would be to replace * by an element type in html.
And lastly there could be chances when you are trying to access elements using //*[contains(#id,'username')], you may be ending up getting a list of these similar WebElements while you might be trying to access only a single at the same time.

Related

Why is fn:escapeXml() adding square brackets to String

I have a Java/JSP webapp that has started showing a bug after I added fn:escapeXml() to a tag that constructs a URL.
The original code was
<p id="provide-link">
Follow this link to see the source data:
<q:link href="../resources/source-data.jsp"
sourceData="${chart.parameters.source}">
Source Data
</q:link>
</p>
q:link is a custom tag that creates a hyperlink out of href as the base and sourceData as a GET parameter. For example, if chart.parameters.source = 'dataset03', then the link embedded in the page is
../resources/source-data.jsp&sourceData=dataset03.
The custom tag q:link is written to handle cases where chart.parameters.source is a Collection of multiple data sources, which is an allowed case.
This code works normally, but is vulnerable to script insertion attacks due to the GET parameter. Thus, I've added the JSTL function escapeXml() to the code to prevent this:
<p id="provide-link">
Follow this link to see the source data:
<q:link href="../resources/source-data.jsp"
sourceData="${fn:escapeXml(chart.parameters.source)}">
Source Data
</q:link>
</p>
When I do this, square brackets get added to the URL:
../resources/source-data.jsp&sourceData=[dataset03]
This is a problem because [dataset03] is not a valid value for the parameter.
The only thing I've found online about this problem is this question, which doesn't provide a full answer but suggests that ${fn:escapeXml(chart.parameters.source)} might be outputting as an Array now that it includes fn:escapeXml(). The theory is that q:link then catches and retains the Array brackets when it converts to String.
Anyone know what's happening and how to stop it?
Your last suggestion seems correct to me, the fn:escapeXml function has an argument of type String. If the parameter passed is a Collection, it will be converted through a toString() method (that add the []). It appends before the fn:escapeXml() call, so it’s not added inside it.
Rem: it doesn’t append without the escapeXml function because your custom tag retrieve and work on the Collection directly (iterating on its values inside probably).
One solution to convert the Collection passed as argument before passing it to the escapeXml fct; through a tag or EL expression (map()...)
Rem2: instead of using a custom tag to encode your url parameters, there are already these standard JSTL tags that do a similar job :
https://www.tutorialspoint.com/jsp/jstl_core_param_tag.htm
https://www.tutorialspoint.com/jsp/jstl_core_url_tag.htm
Hope that will help,
Kind regards,
Cedric

What language is this (think it's Java?), and how do I test (using a browser ide) the math is correct in it?

div(1, sum(1, exp(sum(div(5, product(100, .1)), -5))))
I'm using this in a Solr query, and want to verify that it is the same as :
Where x is 5.
Is this language Java?
If it is, why am I getting this output here:
http://ideone.com/LWYWtU
If it isn't, what language is this and how do I test it?
Thanks in advance for your help.
EDIT: To add more of the surrounding code, here is the full boost value I'm sending to Solr:
if(exists(query({!frange l=0 u=60 v=product(geodist(),0.621371)})),div(1, sum(1, exp(sum(div(product(5), product(100, .1)), -5)))),0)
The reason I think it might be Java is because in the docs, it says Most Java Math functions are now supported, including: and then lists the math functions I ended up using for code.
Solr is Java, but that's not relevant since this is a set of functions that Solr parses and evaluate itself (and not related to Java, except that the backing functions are implemented in Java).
As far as I can say from what you've mapped the functions correctly, as long as the 5 in product(5) is the same as X. You shouldn't need product there, as the value can be included in div directly as far as I can see.
A way to validate it would be to use debugQuery in Solr and see what the value is evaluated as, and then compare it to your own value. Remember that floating point evaluation can introduce a few uncertanities.

Jsoup select - why does it include current element?

I am trying to understand if I'm missing something, because it seems very bizarre to me why Jsoup includes the current element in the search performed by select.
For example (scala code):
val el = doc.select("div").first
el.select("div").contains(el) // => true
What is the point of this? I see very limited cases where you'd actually want this. Do I need to always use el.children.select instead? Is there a nicer method?
Side question: Is there a nicer way to do el.children.select(s).first? In Ruby Nokogiri it would be el.at_css(s) which is much shorter, is there a similar option in Jsoup?
As to why the select method was implemented the way it did, my only guess would be because it's the most straightforward way to do it if we take into consideration the struct that holds the data resulted by your query.
If we think about el, we will see that it is a "tree" representation of the elements that you asked for, having as root the first parent div node. Then you call select on that tree. Now it all depends on how you decide to see this tree. Should we treat this "tree" as a whole (include root) or not (discard root)? It's a matter of taste I guess.
If I judge from myself, a lot of people using Jsoup, probably have had some experience on DOM parsing with jQuery. The equivalent would be something like this $("div").first().find("div") where find is documented as
Get the descendants of each element in the current set of matched
elements, filtered by a selector, jQuery object, or element.
This is in agreement with what you stated. It's just a matter of how the two libraries "see" the resulting tree. Jsoup treats the root as one of the nodes, jQuery differentiates the root (as far find is concerned).
About the second part of your question.
val el = doc.select("div").first
el.children.select(s).first
No there isn't. The only way is to change the css selector.
val result = doc.select("div:eq(0) " + s).first;

using java for replacing text in database

i am using replace method for editing text in mysql database and its working well for
every time i try to replace a string by some other string e.g
REPLACE(Eligibility_Points , '(ii)', 'second point is')";
works well for above case
but does not work well in the following case
REPLACE(Eligibility_Points , '(ii)-(iii)', 'second and third point is')";
how should i fix this problem, thanks for your help
Assuming that this is the MySQL REPLACE string function you are talking about, the only reason I can see why the second example wouldn't work is that (maybe) the Eligibility_Points field (or whatever) doesn't contain the first string at all.
Maybe you could provide more context; e.g. what evidence you have that the replace isn't working.
However #vadchen makes a good point. If you do the replacement in the first example, then it will remove all examples that might trigger a replacement in the second example. Maybe you just need to do the "edits" in the reverse order.
There is no need to escape any of the characters in those fragments, either from the Java or SQL perspective.

XPath getting text from an element after a certain element

So right now if I have something like this:
//div[#class='artist']/p[x]/text()
x can either be 3 or 4, or maybe even a different number. Luckily, if what I am looking for is not in 3, I can just check for null and go on until I find text. The issue is I would rather know I'm going to the right element every time. So I tried this:
div[#class='people']/h3[text()='h3 text']/p/text()
since there will always be a <p> right after <h3>h3 text</h3>. However this never returns anything, and usually results in an error. If I remove /p I will get 'h3 text' returned.
Anyway, how do I get that <p> directly after <h3>?
BTW, I'm using HTMLCleaner in Java for this.
By default when you don't specify an axis you get the child:: axis, which is why the / operator seems to descend the DOM tree child by child. There is an implied child:: after each slash.
In your case you don't want to find a child of the <div>, you want to find a sibling of it. A sibling is an element at the same nesting level. Specifically, you should use the following-sibling:: axis.
div[#class='people']/h3[text()='h3 text']/following-sibling::p/text()
XPath Axes
Axes are an advanced feature of XPath. They are one of the features that make XPath especially powerful.
You're already familiar with one other axis, though you may not have realized it: the # symbol is shorthand for attribute::. When you write #href you're really saying attribute::href, as in look for an attribute called "href" instead of a child.
Axes, eh? Shorthand, eh? Tell me more, you say? OK!
. and .. are shorthand for the more verbose self::node() and parent::node(), respectively. You could use the longer forms if you wished.
The // operator you commonly see as //p or body//a has a hidden descendant-or-self::node() between the slashes. //p is shorthand for /descendant-or-self::node()/p.
Anyway, how do I get that <p>
directly after <h3>?
Use:
div[#class='people']/h3[text()='h3 text']/following-sibling::p[1]

Categories