I have a situation in which i must extract a text and select a row and then i must get a value which is present in a td right next to the cell we selected.The corresponding html page looks like this :
<td id='145'>
<div> MyValue </div>
</td>
<td>
<div>65</div>
</td>
I have selected the cell by using this xpath "div[starts-with(text(),'MyVal')]".I didnt know how to get the value 65 from this.I tried following and following siblings.It didnt work out.Can anyone help me out with this.
You can do following-sibling::td[1] on the parent td element of currently selected <div> MyValue </div>, to get the nearest following td. Then, from this point you can easily return the corresponding child div element :
parent::td/following-sibling::td[1]/div
Related
I have a html like this
<td class="random">
<div id="randomID">Product:</div>
</td>
<td class="random">
<div id="randomID">
<input type="text" class="random">
</div>
</td>
my xpath //div[contains(text(),"Product:")] gives me the first element for which I want to send input. How do I get the input xpath so I can do input.sendkeys() on it.
You use either of the xpath to get the input tag.
Use following and index of input tag
//div[contains(text(),"Product:")]/following::input[1]
Or find the td tag and then use following-sibling
//td[.//div[contains(text(),"Product:")]]/following-sibling::td[1]//input
You can use below xpath as well.
//div[text()="Product:"]/following::input[1]
Or
//td[.//div[text()="Product:"]]/following-sibling::td[1]//input
Use following-sibling
Try with following xpath:
//td[contains(.,"Product:")]//following-sibling::td//input
For the provided code, the input is very simple to identify:
//input[#class='random']
Since I can't see any other duplicate code to force you to be more precise, I don't see the problem in using this one.
If you have multiple , and each or some of them have an input inside, in this case:
//div[text()="Product:"]/following::input[1]
Where Product is the visible text of the div that you want to use as a parent, and then you go straight in the first input taken by its index.
tableenter image description hereI want to write an xpath using axes for the a column value in the table, corresponding to a particular value in the same row.
e.g. I want to print last column value for HDIL.
Xpath that i have tried :
//a[contains (text(), ‘HDIL’)]/following-sibling::td[#class=‘green’]
HTML
<tr>
<td>
<a href="http://demo.guru99.com/">
HDIL </a>
</td>
<td>A</td>
<td>564.6</td>
<td>68.8</td>
<td><font class="green">+ 6.1</font></td>
</tr>
You may find 'table row' with text = 'HDIL' and from this row get the last table cell. E.g. like this:
//tr[td[contains(string(), 'HDIL')]]/td[last()]
or
//tr[td[contains(string(), 'HDIL')]]/td[font]
Or using webDriver I would find all cells for needed row and get the last one:
// C# code;
var lastCell = driver.FindElements(By.XPath("//tr[td[contains(string(), 'HDIL')]]/td")).Last();
Your xpath is written in the way of taking siblings from a but you should take siblings of td that contains a. Your xpath also implies that class='green' is the attribute of td but in fact (according to your example) it is the attribute of font that is child of td.
So the proper xpath would be: //td[a[contains (text(), 'HDIL')]]/following-sibling::td[font[#class='green']].
See test here.
Using Xpath tool from chrome dev tools, I have been able to get xpath string and a td object. I 'm just wondering how do we find the value of text in a td object using selenium python web-drivers?
for your case you can use .gettext() method of selenium.
Once I found that, the attribute within td which contains the text is changing and i was supposed to fetch the text in those attribute.
like
<td class="table__cell" data-col-index="1">
<div class="ec-table__cell-content ">813.75</div>
</td>
<td class="table__cell" data-col-index="2">
<span class="ec-table__cell-content ">522.12</span>
</td>
So to get text within all td in a list, I used dynamic Xpath way:
By.xpath("//td[contains(#class,'table__cell')]/following::*").getText()
Try to search by xpath. Relatively to some ID.
Example of:
id("body_content")/table/tbody/tr/td[3]
in this case will be shown exact elements of 3rd column only.
I need to modify the file table in my trac browser view by creating a class which implements the ITemplateStreamfilter class. I tried using the Transformer from genshi.filters.transform. My table looks like
<tbody>
<tr class="even">
<td class="name">
<a class="partent" title="Parent Directory" ..>..</a>
</td>
..
</tr>
..
</tbody>
I now need to insert a </td> tag just before the frist cell in the first row of the table. The problem is that I only can identify the position of column where I want to put the new cell befor by searching for the "Parent Directory" title: Transformer('//*[#title="Parent Directory"]'). How can I step one tag up than put the new cell before the first <td class="name"> tag?
I'm not THAT familiar with the support for XPATH of Transformer BUT:
What about
Transformer('(//td[*[#title="Parent Directory"]])[1]') and then using the before method?
As far as I understand, this should select the first td node with a child node with an attribute title="Parent Directory".
If you want to select any td with that kind of child node use
Transformer('//td[*[#title="Parent Directory"]]')
However, this only works if Transformer supports those XPATH expressions.
Edit 1
If you're sure, your td has an attribute class="name" you can also use Transformer('(//td[class="name" and *[#title="Parent Directory"]])[1]')
Below is my code
Below is a tag.
<TR id="oldcontent" bgcolor="#D0D0D0">
<TD id="ignore" style="vertical-align:middle">
<input type="checkbox" name="selectedId" value="22047"onclick="updateSelectionList('<%=campaign.getId()%>')">
</TD>
<TD id="oldcontent">Code</TD>
<TD ALIGN="left" id="oldcontent">
<select name="status" style="width=150" id="newcontentformat">
<option value="14" selected="selected">text1</option>
<option value="15">text2</option>
</TD>
<TR>
Here
1)i need to click the checkbox which has dynamically generated value without any string.
2)Can i select the checkbox based on the text "Code" present in next after checkbox?
3)I need to pick up text2 in the dropdown with name status
4)Lastly the issue is this can appear any where in web page,everytime i run the test case.So i need to check the checkbox using String "code",2ndly i need to select value from dropdown which has name staus.There are other dropdown boxes with same name status.So how do i specifically do this?
To get the select list in the row that has the text "Code", you can use the xpath:
//tr[./td[text()='Code']]/td/select
Similarly, for the checkbox, you can use the xpath:
//tr[./td[text()='Code']]/td/input[#type='checkbox']
I believe then the selenium code you want is:
selenium.select("//tr[./td[text()='Code']]/td/select", "text2")
selenium.check("//tr[./td[text()='Code']]/td/input[#type='checkbox']")
As long as there are names available in the HTML, you can use Name to locate an element.
For click on check box you can write:
selenium.click(//input[#name='selectedId']);
To go to check box using text locator should be like:
//div[text()='text you want to precede']/preceding::input[#name='selectedId'];
(here you can use any property instead of name.)
To pickup text from dropdowm:
selenium.select(//select[#name='status'],"text2");
If you use such locators (which are position independent), no matter where your element appears on the screen, you can locate with the help of these locators....
To know more about locating preceding or following Download Ebook:Selenium.1.0.Testing.Tools.Beginners.Guide