xpath retrieve element that occur after parent element - xpath

I need to click on the element that appears after <span class="clientadress">
It can be either <div class="homephone"> or <input type="text">.
I try xpath /following:: or /child:: but it doesn't work.
Is it possible to do something like //following::(div or input)
Example of element order:
<span class="client">
<span class="clientadress">
<div class="homephone">
</span>
or
<span class="client">
<span class="clientadress">
<input type="text">
</span>

Try using
//span[#class="clientadress"]/(following-sibling::div | following-sibling::input)
and see if it works.

Try-Xpath in Firefox gives better results sometimes:
https://addons.mozilla.org/nl/firefox/addon/try-xpath/?utm_source=addons.mozilla.org&utm_medium=referral&utm_content=search

Jack gave me an idea and next is working for me: //following-sibling::*
Thank you all

Related

select div(text) without span xpath

<div class="filters-list-item-content" xpath="4">
<span class="font-500">
Rodzaj konferencji:
</span>
Warsztaty
</div>
<div class="filters-list-item-content" xpath="5">
<span class="font-500"></span>
POH
</div>
I have 6 xpaths and try select all div(text) without text in span I try
//div[#class='filters-list-item-content']/*[not(.//span)]
//div[#class='filters-list-item-content']/SPAN[not(*)]
but it is select only text in span :(
If I understand you correctly, this is what you are looking for:
//div[#class='filters-list-item-content']/text()
Output:
Warsztaty
POH

Select a radio button by text using XPath

<div class="btm0">Question 1
</div><span class="small"></span>
<span id="form2">
<span id="form2:j_idt4598" style="display: none;"></span><ul id="form2:radioButton" class="controls full-width">
<li>
<input class=" firepath-matching-node" name="form2:radioButton" id="form2:radioButton:0" type="radio"><label for="form2:radioButton:0"> Yes</label></li>
<li>
<input class=" firepath-matching-node" name="form2:radioButton" id="form2:radioButton:1" type="radio"><label for="form2:radioButton:1"> No</label></li>
</ul>
<span id="form2:errMessage"></span>
</span>
<div class="btm0">Question 2
</div><span class="small"></span>
<span id="form1">
<span id="form1:j_idt4617" style="display: none;"></span><ul id="form1:radioButton" class="controls full-width">
<li>
<input class=" firepath-matching-node" name="form1:radioButton" id="form1:radioButton:0" type="radio"><label for="form1:radioButton:0"> Yes</label></li>
<li>
<input class=" firepath-matching-node" name="form1:radioButton" id="form1:radioButton:1" type="radio"><label for="form1:radioButton:1"> No</label>
I have this above simple page having a label and option button.
I want to select the 'Question 1 - Yes' using an XPath.
Is there an easy way to create a unique XPath, which will select the correct 'YES' option (which could be Question 1 or Question 2)? I don´t want to hardcode the XPath.
I tried out a partial solution but I only get the second 'YES' option selected:
//*[contains(text(),'Question 1')]/following::label[contains(text(),'Yes')]
Not sure if this is what you are asking, but if you want to limit the scope of your XPath expression to just the 2 label elements after the div, you can use the position() function:
//div[contains(text(),'Question 1')]/following::label[position() <= 2 and contains(text(),'Yes')]
If this is not what you are looking for, please try to clarify your question, perhaps by providing some sample results.
I was able to get some solution after try, but really not happy with the answer as I have to to change my xpath based on YES or NO of option button
for YES :
//*[contains(text(),'Question 1')]/./following::li[1]/label[contains(text(),'Yes')]
for NO :
//*[contains(text(),'Question 1')]/./following::li[2]/label[contains(text(),'Yes')]
As you see in the above solution, I have to toggle the values of li[].
But with above solution, I am able to pinpoint the option.

Need span title in Xpath

I need span title text (RS_GPO) as my xpath output
Here is code:
<TD id="celleditableGrid07" nowrap="nowrap" style='padding:0px;' >`
<DIV class='stacked-row'>
<span id="form(202567).form(TITLE).text" >
<span title='RPS_AEM3'>RPS_AEM3</span>
</span>
</DIV>
<DIV class='stacked-row-bottom'>
<span id="form(202567).form(CONTENT).text" >
<span title='RS_GPO'>RS_GPO</span>
</span>
</DIV>
My intention for xpath is I want catch text “RS_GPO” in to a variable.
Because this is system generated text.
Thanks in Advance.
//span[#title='RS_GPO']
OR
//div[#class='stacked-row-bottom']/span[#id='form(202567).form(CONTENT).text']/span[#title='RS_GPO']
//span[contains(#id,'form(CONTENT).text')]/span
If you want the content of the title attribute instead of the element's text content, then:
//span[contains(#id,'form(CONTENT).text')]/span/#title

How to simulate click in span using Mechanize Ruby?

I have a webpage with list of pages:
<div class="pager">
<span class="current_page">1</span>
<span class="page" samo:page="2">2</span>
<span class="page" samo:page="3">3</span>
<span class="page" samo:page="4">4</span>
<span class="page" samo:page="5">5</span>
<span class="page" samo:page="6">6</span>
<span class="page" samo:page="7">7</span>
<span class="page" samo:page="8">8</span>
<span class="page" samo:page="9">9</span>
<span class="page" samo:page="10">10</span>
<span class="page" samo:page="11">11</span>
</div>
How can I click on the span using mechanize?
According to this ASCIIcasts you can perform searches and findings:
There are two methods on the page object that we can use to extract
elements from a page using Nokogiri. The first of these is called at
and will return a single element that matches a selector.
agent.page.at(".edit_item")
The second method is search. This is similar, but returns an array of
all of the elements that match.
agent.page.search(".edit_item")
http://asciicasts.com/episodes/191-mechanize
So doing something like:
agent.page.at(".page")
Will return the array of spans. And then you will be able to work with them and just do the #click action.
EDITED:
As long as the span is a non interactive element, and click is a Link action, you will have to find a workaround:
How to click link in Mechanize and Nokogiri?

Xpath: locate a node by multiple attributes of a parent node

Here is the code:
<li class="abc">
<div class="abc">
<input type="checkbox">
</div>
<div class="xyz">
<div class="headline">Mongo like candy</div>
<div>
</li>
<li class="abc">
<div class="abc">
<input type="checkbox">
</div>
<div class="xyz">
<div class="headline">Candygram for mongo</div>
<div>
</li>
Xpath challenge. I want locate the checkbox of the li which contains the headline "Mongo like candy" so I can select it using Selenium. In other words, how do you locate the checkbox from here:
li//div[#class='abc']//input[#type='checkbox']
but qualifying it with a different attribute within the same parent node:
li//div[#headline][contains(text(),"Mongo like candy")]
The basic idea is to qualify the final path with a predicate, i.e.
li[/*predicate here*/]//div[#class='abc']//input[#type='checkbox']
The predicate expresses the condition on the li that you want:
.//div[#class='headline' and contains(text(), "Mongo like candy")]
Putting them together yields:
li[.//div[#class='headline' and contains(text(), "Mongo like candy")]]//div[#class='abc']//input[#type='checkbox']
something like
li[div[#class='xyz']//div[#class='headline' and contains(text(),"Mongo like candy"))]]//input[#type='checkbox']
unless I messed up parentheses. (that is, you select not just li, but the proper li).
Even this works:
//li[1]/div[1]/input[#type='checkbox']
It may fail if more div tags are introduced in the page.

Resources