Selenium Webdriver - XPath: How to use 'and' 'Or' in xpath - xpath

I want to create XPath as //a[class='btn btn-invisible btn-link routeLink'] and //a[data-route='#Leads'].
Other example: //div[#class='btn-group'] and //a[#text()='Leads']
HTML:
<div class="btn-group" style="display: none;">
<a class="btn btn-invisible btn-link routeLink" data-route="#Leads" href="#Leads">Leads</a>

//a[#class='btn btn-invisble' and data-route='#Leads']
In the second example: Do you want a div or do you want an anchor? Shouldn't those be different locators?
You can or them as such using a pipe: //div[#class='x']|//a[#text='aa'].

I think below is what you need:
1) //a[#class='btn btn-invisible btn-link routeLink'][#data-route='#Leads']
2) //div[#class='btn-group'][text()='Leads']
and if you really need to use 'and' then niharika_neo's suggestion should work too.

Related

xpath retrieve element that occur after parent element

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

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.

How to use preceding sibling in xpath

Here i want to Accept button. Here is the HTML.
<div class="friend-request no-pad ng-scope" ng-if="notifications.friendInvites.length > 0">
<p class="rem-head mzero small">
<div class="reminder-lst lst-box ng-scope" ng-repeat="friendInvite in notifications.friendInvites | limitTo:limit">
<span class="img-frame img-circle">
<span class="pull-left rem-detail-a">
<a class="pull-left rem-detail-a pzero" href="friend#/friends/friendprofile/b6c70e4f-bfe1-440d-836c-2e8fdc88540e">
<span class="frndact pull-right">
<a class="ignore" ng-click="ignoreNotification(friendInvite, 'friend')" href="javascript:void(0)">
<a class="accept" ng-click="acceptNotification(friendInvite, 'friend')" href="javascript:void(0)">
<i class="fa fa-lg fa-check-circle green"></i>
</a>
I have tried using below xpath but not working. Can anyone plz help me?
#FindBy(xpath=".//a[ng-click='acceptNotification(friendInvite, 'friend')']/preceding-sibling::i[#css='.fa.fa-lg.fa-check-circle.green']").
Thanks in advance
Assuming that you are looking for the 'A' tag of class accept, you can try
//i[#class="fa fa-lg fa-check-circle green"]/preceding-sibling::a[#class="accept"]
or
//i[#class="fa fa-lg fa-check-circle green"]/preceding-sibling::a[#ng-click="acceptNotification(friendInvite, 'friend')"]
a couple of things:
as TT noted your xpath was missing the # for the attribute selector
the sample you posted is not a well formed xml, expect troubles with xpath if you don't have an xhtml compliant source.
if you use the second example mind to escape either the " or the ' quotes, if you use it inside another expression

Dynamic xpath using anchor tag

Can any please help me in finding the dynamic xapth?
I have tried with,
#FindBy(xpath="//a[contains(#href,’friend#/friends/myfriends/friendsrequest’)]")
public WebElement lnk_FrndsSeeAll;
Here is the HTMl code,
<a class="pull-right see" href="friend#/friends/myfriends/friendsrequest">See all</a>
you can following xpath, which I'm taking using text in the anchor tag
//a[contains(text(),'See all')]
See all should be constant always, then only above xpath will work
It was working fine with beloew code,
#FindBy(xpath="//a[startswith(#href,'friend#/friends/myfriends/friendsrequest')]‌​‌​")
<a class="pull-right see" href="friend#/friends/myfriends/friendsrequest">See all</a>
<a class="pull-right see" href="friend#/friends/myfriends/friendsrequest">See all</a>

How to get xpath to a link to picture?

How to extract xpath to link of image? I want to extract this specific link:
http://insales.ru/images/bigpic.jpeg
I dont know how I must specify xpath. Do I have to include all parent tags to get it or I just can go directly to tag?
<div class="tango">
<div class="container-horizontal">
<div class="clip-horizontal">
<ul id="carousel" class="pagination jcarousel">
<li class="jcarousel-item">
<a rev=http://insales.ru/images/bigpic.jpeg href="http://insales.ru/images/pic2.jpeg">
<img src="http://insales.ru/images/thumb.jpeg">
</a>
</li>
</ul>
</div>
</div>
</div>
So my xpath would be:
//li[contains(#class, 'jcarousel-item jcarousel-item-horizontal jcarouse')]/a
Or I have to include all parent div tags:
//div[#class="tango"]//div[#class="container-horizontal"]//li[contains(#class, 'jcarousel-item jcarousel-item-horizontal jcarouse')]/a
But anyway both of this xpaths don't work.
How to specify xpath to extract this link: http://insales.ru/images/bigpic.jpeg
There are several options but the shortest one is probably:
.//*[#id='carousel']/li/a/img

Resources