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.
Related
I have a condition where tr row which generates dynamic value:
<tbody>
<tr id="24686" tabindex="0">
<td class="nowrap xh-highlight" style="padding: 3px 8px;">Available</td>
</tr>
</tbody>
I have Xpath 1: (//tbody/tr/td[contains(text(),'Available')])[1] which returns
Available
and Xpath 2: //tr[1]/#id which returns
ld_9050427
22707
The condition is that I want to generate one xpath which will return first number whose status is Available and then return its ID. Later on I want to use this same id to carry on later process?
I tried something like below but it didn't work
(//tbody/tr[/#id and/td[contains(text(),'Disponible')]])[1]
If you want to select tr that has id attribute (any) and table cell with text "Available" try
//tr[#id and td='Available']
to extract id value for further use you need get_attribute/getAttribute method
To find the first number whose status is Available and then return its ID you can use the following solution:
xpath:
"//tbody//tr//td[text()='Available']/.."
Note 1: The .. in the xpath refers to the ancestor node
Note 2: As you are looking for the first match with the implemented condition, you have use either:
Python:
find_element_by_xpath()
Java:
findElement()
C#:
FindElement()
Note 3: Finally you have to use getAttribute("id") / get_attribute("id") to extract the value of the id attribute as follows:
I want to find the following item using Selenium. The value of the class changes whenever there is a change. This is inside a complex page (multiple iframes, and other items loaded dynamically). The only unique id is itemid, which is dynamic value and title combination. If I click on this Action, am getting another new set of complex items. I am new to Selenium. How to do that?
HTML:
<td itemid="xxyyy.as123" title="Actions" nowrap="" class="text-button">Actions <img src="../row.gif"></td>
<td itemid="xxyyy.as123" title="Actions" nowrap="" class="text-button button-active">Actions <img src="../row.gif"></td>
<td itemid="xxyyy.as123" title="Actions" nowrap="" class="text-button button-hover">Actions <img src="../row.gif"></td>
The code I tried:
Find by Xpath
var element=driver.FindElement(By.XPath("html/body/div[id='pageContent']/iframe/#document/html/frameset/frame[name='detailsDisplay']/#document/html/body/form[name='tableForm']/div[id='divToolbarContainer']/div[id='divToolbar']/div[1][class='toolbar']/table/tbody/tr/td[title='Actions']"));
Find by Link Text
var element = driver.FindElement(By.LinkText("Actions"));
Any help would be appreciated.
Try
By.CssSelector("td[title="Actions"]");
By.CssSelector("td[itemid="xxyyy.as123"]");
By.CssSelector("td[itemid="xxyyy.as123"][title="Actions"]")
Create Dynamic CSS Selector.
For Example:
driver.FindElement(By.CssSelector("td[itemid$="xxyyy."]")).Click();
Note: In dynamic Elements, there is always a part of locator wich is fixed. we need to generate the locator using this part.
If fixed part is at starting - Use Carrot Character (^)
If fixed part is at Middle - Use Asterisk sign (*)
If fixed part is at End - Use Doller sign ($)
Finally I was able to achieve it, by using the frame names.
driver.SwitchTo().Frame("content").SwitchTo().Frame("detailsDisplay");
var element = driver.FindElement(By.XPath("//*[#id=\"divToolbar\"]/div[1]/table/tbody/tr/td[1]"));
Thanks everyone.
I've tried several xpath's for choosing the dropdown list. But nothing is worked.
Some of the xpath's I used are as follows:
By.xpath("//table/tbody/tr[2]/td[2]/span").click;
Or
By.xpath("/td[2]/span[contains(text(),'NATIONAL IDENTITY DOCUMENT')]").click;
Please find the below html tags, I need to select the value either 'ASYLUM SEEKER PERMIT DOCUMENT' or 'NATIONAL IDENTITY DOCUMENT'.
<tbody>
<tr id="jP2Qrg" class="z-comboitem">
<td class="z-comboitem-img"/>
<td class="z-comboitem-text">
<span class="z-comboitem-spacer"/>
ASYLUM SEEKER PERMIT DOCUMENT
</td>
</tr>
<tr id="jP2Qsg" class="z-comboitem z-comboitem-over">
<td class="z-comboitem-img"/>
<td class="z-comboitem-text">
<span class="z-comboitem-spacer"/>
NATIONAL IDENTITY DOCUMENT
</td>
</tr>
</tbody>
When I try to take xpath using firepath, it is dynamic. Every time the xpath and the id is keep changing. So please suggest the xpath which works.
The text is not inside the span text and it is in second td tag. you can try with small change in your code as given below.
By.xpath("//table/tbody/tr[2]/td[2]").click;
or
By.xpath("//td[contains(text(),'NATIONAL IDENTITY DOCUMENT')]").click;
Can you check this..,
By.xpath("//*[contains(text(),'NATIONAL IDENTITY DOCUMENT')]").click;
By.xpath("//*[contains(text(),'ASYLUM SEEKER PERMIT DOCUMENT')]").click;
Try to use below XPath to match required option:
By.xpath("//td[normalize-space()='ASYLUM SEEKER PERMIT DOCUMENT']").click;
or
By.xpath("//td[.='ASYLUM SEEKER PERMIT DOCUMENT']").click;
As #Murthi said, text node is not a child of span, but td. Note that there are some specifics in locating text nodes
Here is the Answer to your Question:
To click on ASYLUM SEEKER PERMIT DOCUMENT you can use the following xpath:
By.xpath("//td[contains(.,'ASYLUM SEEKER PERMIT DOCUMENT') and not (#class='z-comboitem-spacer')]").click;
Let me know if this Answers your Question.
How about this xpaths,
Updated code
(//td[2])[1]//*[starts-with(#class,"z-com")]; // Selects ASYLUM..
(//td[2])[2]//*[starts-with(#class,"z-com")]; // Selects NATIONAL..
This should work if it doesn't have same class name in the same position.
Old xpath's
driver.findelement(By.xpath("(//td[2])[1]")); // selects ASYLUM SEEKER PERMIT DOCUMENT
driver.findelement(By.xpath("(//td[2])[2]")); // selects NATIONAL IDENTITY DOCUMENT
Find out the solution for selecting the dropdown options.
By using List method, I took the list of options available in that field and selected by using clicking the index value of list.
List<WebElement> Idtype = driver.findElements(By.xpath("//tr[2]/td[2][#class = 'z-comboitem-text']"));
Idtype.get(0).click();
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 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