What will be the xpath for below expression - selenium

<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()]

Related

Selenium XPath multiple selection

I want to find all elements in the table that match the following conditions:
-div text contains '2019';
-div class='excellent';
here is the HTML code excerpt:
<tr>
<td>Name of Person1</td>
<td>
<div class="testDate">21/12/2019</div>
<div class="excellent"></div>
</td>
</tr>
<tr>
<td>Name of Person2</td>
<td>
<div class="testDate">01/12/2017</div>
</td>
</tr>
I tried this solution:
//tr/td[2][div/text()='21/12/2019'][div[#class='starred']]
but I need the year only and not entirely date.
Use the below xpath.
//tr/td[div[contains(.,'2019')]and div[#class='excellent']]
Screenshot:

How to locate the parent element of a child element identified through innerText?

I need to select a row in a table where I have a text, hence I would like to leverage the option of selecting a text and then eventually selecting the parent
Now the page looks like:
<tr class=" tableRow1" id="_pu5ufb" dr="1" _awtisprimaryrow="1">
<td width="1" class="tableBody w-tbl-cell" align="center"><span
class="selectColumnMarker">
<div class="w-chk-container">
<input bh="CHKINP" hasaction="false" class="w-chk-native"
id="_m7iynb" value="1" type="checkbox" elementid="_jw4lmb"
issender="false" awnomitcomponent="true" name="_jw4lmb"><label
bh="CHK" class="w-chk w-chk-dsize"></label>
</div>
</span>
</td>
<td align="left" class="tableBody w-tbl-cell">
<span>
<table role="presentation" class="mls" cellpadding="0"
cellspacing="0">
<tbody><tr>
<td class="" id="_tz87e" tabindex="0"><a id="_3iqrbb" href="#"
bh="HL" _sf="true">Analyst</a>
</td>
</tr>
</tbody></table>
</span>
</td><td class="tableBody w-tbl-cell">
</td>
<td class="tableBody w-tbl-cell">
</td>
</tr>
I need to find the text Analyst and then find the associated <tr> class and select the <tr> class.
Any help would be highly appreciable
First, whenever you have mixed content (text and markup) it is better to compare elements' string value than text nodes because inline markup might be splitting the compared text into different text nodes.
Second, you can use:
//tr[td='Analyst']/#class
Note: node-set comparison is an existencial comparison. It means that you are asking if there is some node (some td element in this case) with string value equal to 'Analyst'.
Of course, in HTML there are elements for which white space is not significant for rendering (it's not preserved) despite its presence in the source document. In that case you can use this simple XPath 1.0 expression:
//tr[td[normalize-space()='Analyst']]/#class
Do note: a node-set has a false boolean value if and only if it's empty; you can "nest" predicates (properly, a predicate can be any XPath expression).
I need to find the text Analyst and then find the associated tr class and select the tr class.
XPath 2.01
This XPath,
//tr[td/normalize-space() = "Analyst"]/#class
will select all #class attributes of tr elements containing a td with a space-normalized string value of "Analyst".
Do note, however, that in your sample HTML, such a tr has no #class.
1Thanks for correction, #DebanjanB
Fix your XML file to
<?xml version="1.0"?>
<!DOCTYPE stylesheet [
<!ENTITY nbsp " ">
]>
<root>
<tr class=" tableRow1" id="_pu5ufb" dr="1" _awtisprimaryrow="1">
<td width="1" class="tableBody w-tbl-cell" align="center">
<span class="selectColumnMarker">
<div class="w-chk-container">
<input bh="CHKINP" hasaction="false" class="w-chk-native" id="_m7iynb" value="1" type="checkbox" elementid="_jw4lmb" issender="false" awnomitcomponent="true" name="_jw4lmb"/>
<label bh="CHK" class="w-chk w-chk-dsize"/>
</div>
</span>
</td>
<td align="left" class="tableBody w-tbl-cell">
<span>
<table role="presentation" class="mls" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="" id="_tz87e" tabindex="0">
<a id="_3iqrbb" href="#" bh="HL" _sf="true">Analyst</a>
</td>
</tr>
</tbody>
</table>
</span>
</td>
<td class="tableBody w-tbl-cell">
</td>
<td class="tableBody w-tbl-cell">
</td>
</tr>
</root>
Then, the expression you are looking for is
//tr[td/a/text()='Analyst']/#class
But because the tr element does not have a class attribute, the result is empty.
A bit unclear what exactly you meant by ...find the associated tr class and select the tr class... once you have found ...the text Analyst....
However, as the elements are dynamic element and to locate the element with text as Analyst you can use either of the following Java based Locator Strategies:
linkText
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Analyst")));
cssSelector:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("td.tableBody table.mls a[bh='HL']")))
xpath:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//td[contains(#class, 'tableBody')]//table[#class='mls']//a[text()='Analyst']")));
To extract the class attribute of the <tr> element with respect to the text as Analyst you can use the following Java based solution:
xpath:
String tr_class_attrib = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[contains(#class, 'tableBody')]//table[#class='mls']//a[text()='Analyst']//preceding::tr[1]"))).getAttribute("class");

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: ']]

Need help finding xpath of input field beside label

I have a dom structure like this
<tr class="">
<td class="labelCol requiredInput">
<label for="name_lastacc2" class="">
<span class="requiredMark">*</span>
Last Name</label>
</td>
<td class="dataCol col02">
<div class="requiredInput">
<div class="requiredBlock"></div>
<input id="name_lastacc2" maxlength="80" name="name_lastacc2" size="20" tabindex="3" type="text"></div>
</td>
<td class="labelCol">
<label for="00NG0000008Rybp">Business Ext.</label>
</td>
<td class="dataCol">
<input id="00NG0000008Rybp" maxlength="10" name="00NG0000008Rybp" size="20" tabindex="22" type="text"></td>
</tr>
I am trying to find the input box next to its label.
Eg:
Last Name: ________________
I have tried using xpath queries like
//label[contains(text(),'Last Name']/../following-sibling::*/div/input[#type='text']
but i get an [INVALID XPATH EXPRESSION] error. Where am i going wrong ?
Your path expression is missing the closing bracket of contains().
//label[contains(text(),'Last Name')]/../following-sibling::*/div/input[#type='text']
But that said, I do not think your path expression is correct now. Your expression will match any following sibling of the parent of label[contains(text(),'Last Name')]. What you want it to find is the input element that immediately follows.
The only reason your original expression only finds one input element is that the second one is nested inside one more div. But I do not think you should rely on that.
Instead, try:
//label[contains(.,'Last Name')]/following::input[1]
Then, the result is
<input id="name_lastacc2" maxlength="80" name="name_lastacc2" size="20" tabindex="3" type="text"/>

How to search by first td text and clicking in seconds td href by JUnit, Webdriver?

<div class="row">
<div class="row">
<table>
<tbody>
<tr>
<td class="list-text">
<div style="background-color: transparent;">Testing this function</div>
</td>
<td class="list-buttons" rowspan="2">
Config
<a class="edit-button" href="javascript:;">Edit</a>
<a class="save-button" href="javascript:;" style="display:none;">Save</a>
Delete
</td>
</tr>
<tr>
</tbody>
</table>
</div>
I need to find the element which contains the text "Testing this function" and click to link "Config".
Can't find a way to do it.
Edited: Found a way to do it:
By.xpath("//div[#class='row']/table/tbody/tr[td/div[contains(.,'Testing')]]/td[2]/a[contains(.,'Config')]")
But maybe someone has a better solution for this?
If you have to use text, my preference would be;
By.xpath("//div[contains(text(),'Testing')]]//a[contains(text(),'Config')]") .
I prefer using "text()" to "." just for readability. Apologies if "." is actually a better approach which I wasn't aware of.
Also unless you have a specific reason, I would avoid using tightly coupled xpath. E.g. I have not used " td[2]", instead I have said the"a" can be anywhere within in the "div"