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"/>
Related
I've added kendo-grid like this:
https://www.telerik.com/kendo-vue-ui/components/grid/selection/#toc-checkbox-selection
But when I investigate my source code under console I don't see tags "id" and "for"
<td role="gridcell">
<input class="k-checkbox" data-role="checkbox" aria-label="Select row" aria-checked="false" type="checkbox">
<label class="k-checkbox-label k-no-text"></label>
</td>
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: ']]
I'm adding <input> fields in form using jquery html() on click event.
$('#container').html('<input type="text" name="ce" value="some" >');
after submitting php POST i'm unable to see index ce with print_r($_POST);
is there any special method to add elements to dom for this ? please advise.
Solved ! Thanks for answering. in my scenario even with append it didn't work. now it's done with my previous code using html(). Problem was wrapping the <form>. my table is large i'm only pointing out the problem. my structure was suppose to like:
<table>
<tr>
<form id="myform">
<td><input type="text" name="name" ></td>
<td>
<div id="container">
<!-- dynamically generated inside this div against it's id (which didn't work) -->
<input type="text" name="ce" >
</div>
</td>
</form>
</tr>
</table>
i simply Put the entire table into the 'form' tags
<form id="myform">
<table>
<tr>
<td><input type="text" name="name" ></td>
<td>
<div id="container">
<input type="text" name="ce" > <!-- dynamically generated inside div (works!) -->
</div>
</td>
</tr>
</table>
</form>
it worked perfectly with both append() and html(). hope will be helpful for someone.
When you do $('#container').html you are not adding a new input, you are replacing all your content with this new input..
try $('#container').append tag
Look at this example -> https://jsfiddle.net/660a3t1g/
$('#myInputs').append('<input type="text" placeholder="LastName: " name="some" >');
<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()]
i faced a problem while replaying a script created for yahoomail page.The XPath statement to enter value in the "To" text field , is not working.Following are the XPath statements i used.
At the first try i used
<tr>
<td>type</td>
<td>to</td>
<td>mgtest#ymail.com</td>
</tr>
Second try was this statement
<tr>
<td>type</td>
<td>//div[#id= 'toid']/textarea[#id= 'to'][#name= 'to']</td>
<td>mgtest#ymail.com</td>
</tr>
Third try was this
<tr>
<td>typeKeysAndWait</td>
<td>//div[# id= 'composebox']/div[#id= 'toid']/textarea[#id= 'to'][#name= 'to']</td>
<td>mgtest#ymail.com</td>
</tr>
The result was like
[error] Element //div[# id= 'composebox']/div[#id= 'toid']/textarea[#id= 'to'][#name= 'to'] not found
similar result was obtained in the previous attempts.
Later when i tried
<tr>
<td>type</td>
<td>css=textarea.txtfield</td>
<td>mgtest#ymail.com</td>
</tr>
mail id was entered into the text field ,and the script worked perfectly.what might be the reason.Any thoughts.?
I am adding the XPath statements
<div id="composepage">
<div id="composebox" class="roundcorner">
<div id="errorContainer"/>
<input type="hidden" name="defFromAddress" value="mgtest#ymail.com"/>
<div class="fields row">
</div>
<div id="toid" class="row">
<label id="compose_to" for="to">
</label>
<textarea id="to" class="txtfield" name="to" autocomplete="off" tabindex="1" style="overflow: hidden; height: 19px;"/>
</div>
You have written invalid xpath query.
It should be
//div[# id='composebox']/div[#id='toid']/textarea[#id='to' and #name='to']
The <textarea> has an id attribute, which should be unique, so your first locator of simply to should work. It's possible that the element is not present or visible when your selenium command executes. I would recommend the following:
waitForVisible | id=to | 60000
type | id=to | mgtest#ymail.com
If your elements have unique ids, and you need to use XPath, you only need to be relative to the closest element with an id attribute.