selenium xpath to select following-sibling - selenium

I have the following HTML:
<tr class="k-grouping-row" role="row">
<td aria-expanded="true" colspan="6">
<p class="k-reset">
<a class="k-icon k-i-collapse" tabindex="-1" href="#"></a>
<span class="consolidation-group" style="font-weight: bold;" data-key="aedfdb66-bb11-4350-9d25-21941820141b">jhmfgjf (2 Items)</span>
</p>
<a class="k-button unconsolidation-link" onclick="UnConsolidateGroup("aedfdb66-bb11-4350-9d25-21941820141b", "8780f45d-0e81-4f5c-b206-61b682b27d67")" title="Unconsolidate all matter entries in this group">
<span class="marginRight5 icon-unlink"></span>
Unconsolidate All
</a>
</td>
I would like to click on the span "Unconsolidate All" using following-sibling operator. I tried the following code:
//span[contains(text(), 'jhmfgjf')]/../following-sibling::class[#title='Unconsolidate all matter entries in this group']
But it does not work, the first part does work, it's just the following-sibling part that does not work.
Any help would be helpful

The element's name is <a>, not <class>.
You should do the following:
//span[contains(text(), 'jhmfgjf')]/../following-sibling::a[#title='Unconsolidate all matter entries in this group']

Unconsolidate All is not a span element. Its a "a" element.
You can use
//a[contains(text(),'Unconsolidate All')]
And if you specifically want to use following-sibling , you can use in this way
//span[contains(text(), 'jhmfgjf')]//following-sibling::a[#title='Unconsolidate all matter entries in this group']
For reference and learning about sibling, ancestors in xpath , you can see click here

Related

How can I find the dynamic xpath for the word "Pace" in the attached?

I need the dynamic xpath for the the word "Pace" in the below code It is the the name of the third column in a table.
I have tried the following:
//table[contains(#class,'data-grid-table data-grid-fixed-row-table')]//span[contains(#class,'lightning-table-cell-measure-header-value')][contains(text(),'Pace')]
but this seems to be too long of a locator for my test. Is there a shorter dynamic xpath locator that I can use? Here is the snippet of the code that contains the word:
<th data-row-index="0" data-column-index="0" data-fixed-row="true" data-fixed-column="false" aria-selected="false" aria-hidden="false" id="data-grid-24-fixedrow0-col0" class="data-grid-table-cell data-grid-table-cell-start data-grid-header-cell" tabindex="0">
<div class="data-grid-table-cell-box" style="height: 17px;">
<div class="wave-table-cell-measure-header">
<span class="wave-table-cell-measure-header-text" data-tooltip="Pace" data-tooltip-on-truncation="true">
<span class="lightning-table-cell-measure-header-value" tabindex="-1">
<span class="header-icon-container">
<span class="slds-icon_container">
<svg aria-hidden="true" class="header-icon-sprite slds-icon">
<use xlink:href="/images/sprite.analytics.svg#formula"></use>
</svg>
<span class="slds-assistive-text">
Formula
</span>
</span>
</span>
Pace
</span>
</span>
</div>
</div>
<div class="data-grid-resize-slider" style="position: absolute; width: 0px; height: 0px;">
<input class="data-grid-resize-slider-input" aria-label="Pace Column Width: 115" type="range" tabindex="-1" min="35" max="1000" value="115">
</div>
</th>
The issue may be with the double [][] notation you are using for your //span element at the end. I've never seen that type of XPath syntax before, and I've always grouped queries into a single set of [].
You can use the following XPath:
//th/div/div//span[contains(text(), 'Pace']
This will get you the span element containing text 'Pace'.
If you just want to fix your existing XPath syntax, this might work too:
//table[contains(#class,'data-grid-table data-grid-fixed-row-table')]//span[contains(text(),'Pace') and contains(#class,'lightning-table-cell-measure-header-value')]
I've combined your last two queries into one using and operator.

xpath with Multiple condition in Selenium

Wants to locate element with multiple condition on particular row,
I want to Click on div which contains ng-click="condition001" which has parent link contains Auto-001
So query become like : Select element xpath("//div[contains(#ng-click,'condition001')]") where Link("Auto-001")
<tr ng-repeat="(abc, xyz)" ng-show="data.length > 0">
<td class="x001">
<div class="x002">
<a class="x003" href="#">Auto-001</a>
</div>
</td>
<td class="x004">
<div class="x005">
<div class="x006">
<div class="x007" ng-click="condition001" tabindex="0">
<i class="x008"></i>
</div>
</div>
</div>
</td>
</tr>
Please suggest relevant xpath code which can work for above criteria,
To click on the WebElement identified through xpath as ("//div[contains(#ng-click,'condition001')]") where it has partialLinkText as ("Auto-001") you can use the following line of Java code :
WebElement myElem = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='x003' and contains(.,'Auto-001')]//following::td[1]//div[#class='x007' and #ng-click='condition001']")));

How to extract text between 2 same child tags of a parent tag

Below is the html markup and I want to extract mobile number 7788996655 using xpath:-
<table class="table hover" id="resultTable">
<tbody>
<tr class="odd">
<td class="left">
<img src="/symfony/web/index.php/pim/viewPhoto/gamerNumber/1/from/gameDir" height="200" width="196">
</td>
<td class="left">
<span style="font-weight:bold;">John Player</span>
<br>
<span style="font-weight:bold;">Email: </span>
john.player#yyeett.com,
<span style="font-weight:bold;">
Mobile: </span>7788996655,
<span style="font-weight:bold;">Skype: </span>
game.player,
<br>
<span style="font-weight:bold;">Designation: </span>
Moderator,
<br>
<span style="font-weight:bold;">Game Name: </span>
Counter Strike
<br>
</td>
</tr>
</tbody>
</table>
I tried below xpath, but it didn't worked:
//td/span[contains(.,'Mobile:')]/following-sibling::text()[1]
Can someone provide solution for this?
Workaround I tried is to get whole text within td and then used pattern matching regex to extract mobile number from it. But, now i want to know whether is it possible using xpath?
You can do it using below XPath
<parent td node xpath>/text()[preceding-sibling::span[1][text() = 'Mobile: ']]
Now for an explanation. /td/text() will select each text which is part of the td node but is not part of any other node. So in this case it ,,7788996655,, game.player, moderator.
Now out of these 4 text we want to filter out one which has a previous sibling, which is a span and has text Mobile:, so we can add [preceding-sibling::span[1][text()='Mobile: ']]

Unable to locate click the check box

Please help me the locate the check box and select. There are multiple check boxes, there is no way to locate them uniquely.
Here is the Code for one of such check box.
Thanks in Advance for the Help!!!
<div class="col-xs-2 col-sm-2 col-lg-2" style="height:65px">
<ul class="list-inline pull-right">
<li>
<md-input-container class="md-block">
<md-checkbox value="$index+1check" class="checkbox ng-valid ng-dirty ng-touched ng-empty" ng-model="item.Selectedd" ng-click="toggle($index+1, selected,item.TitleId,item)" icon,md-checkbox.md-checked._md-icon="{background-color: green;}" id="Cbk_List" role="checkbox" tabindex="0" aria-checked="false" aria-invalid="false" style=""><div class="_md-container md-ink-ripple" md-ink-ripple="" md-ink-ripple-checkbox=""><div class="_md-icon"></div></div><div ng-transclude="" class="_md-label">
</div></md-checkbox>
</md-input-container>
</li>
<li>
<div class="manageTitle_CirclCard">
<div class="ng-binding">2</div>
</div>
</li>
</ul>
</div>
You should try to locate using cssSelector with it's attribute tabindex as below :-
#Cbk_List[tabindex='0']
Edited :- If checkbox element has not any attributes with unique value, you should try using xpath with index, assuming you want to get first checkbox element, then try below xpath :-
(.//*[#id = 'Cbk_List'])[1]
Note :- In above xpath, just change the index from 1 to desire one checkbox to find.

What will be the xpath for below expression

<tr>
<td>
<span class="dijitArrowNodeInner" data-dojo-attach-point="arrowNodeInner"/>
</td>
<td>
<div class="dijitTitlePaneTextNode" data-dojo-attach-point="titleNode" style="-moz-user-select: none;">
<div>Scripts</div>
</div>
</td>
</tr>
What will be the xpath for the above expression to get the 'Script'.
If by "the Script" you mean the text content of the inner div element:
/tr/td/div/div
Else, if you mean the div element that has "Scripts" as its content:
//div[. = 'Scripts']
But I doubt this is of much help unless you explain exactly what you need.
From your sample provided, this should work:
//tr/td/div[#class='dijitTitlePaneTextNode']/div[text()]