How to create conditional xpath in selenium? - selenium

My HTML code looks like:
Table 1: And Table 2
<table id="ContentPlaceHolder_Body0">
<tbody>
<tr align="Center">
<td style="width:250px;">Some Name</td>
</tr>
</tbody>
</table>
<table id="ContentPlaceHolder_Body1">
<tbody>
<tr align="Center">
<td style="width:250px;">Some Name</td>
</tr>
</tbody>
</table>
When I use //*[contains(text(),'Some Name')] this xpath selects 2 values while I need to select only one value by Text.
I need to use that xpath with Text.
I'm trying to create xpath to get the "Some Name" from Table 2, element locator by using Text.
So I created this Xpath:
//table[#id='ContentPlaceHolder_Body1']//tr//td[#text()='Some Name']
This xpath is not working, need help to create correct xpath.

Given your HTML, this XPath works for me
//table[#id='ContentPlaceHolder_Body1']//td[text()='Some Name']
I think the issue with your XPath is that you were using #text() when it should be text().
//table[#id='ContentPlaceHolder_Body1']//tr//td[#text()='Some Name']
^ remove the #

You need to just remove the #
//table[#id='ContentPlaceHolder_Body1']//tr//td[text()='Some Name']

Related

Click on a table link in selenium webdriver

Please find the sample code below
<tbody>
<tr>
<td class="menubox">
<table id="menubar" border="0">
<tbody>
<tr id="mytr">
<td class="level1" id="panel" onclick="popupwin onmouseover="this.classname onclick="popupwin"> menu1</td>
<td class="level1" id="panel" onclick="popupwin onmouseover="this.classname onclick="popupwin"> menu2</td>
<td class="level1" id="panel" onclick="popupwin onmouseover="this.classname onclick="popupwin"> menu3</td>
I am tring to access the menu3 column which is a link by xpath
//table[#id='menubar']/tbody/tr/td[3] but throwing an error no such element
found. could any one suggest what is wrong in the xpath ?
can you please try this:-
//tr[#id='mytr']//td[3]
Not sure why your Xpath wasn't working, your table structure and xpath is matching together.
However please try following xpath which might work. and if the website your testing is public. please provide the link.
//tr[#id='mytr']/td[contains(text(),'menu3')]

Selenium XPath - Ignore element amongst table

I have an issue with selecting a table to be read in Selenium.
I have a table in which there is two 'tr' elements inside the 'thead', and I need to find a way to ignore the first of these.
Here is the code:
<table class="noselect">
<thead>
<tr>
<th> </th>
<th class="number IOL">Interest Rates</th>
<th class="number IO">
</tr>
<tr>
<th>Description</th>
<th class="number">Value</th>
<th class="number">Percentage</th>
</tr>
</thead>
<tbody>
<tr>
<tr class="">
<tr class="">
<tr class="">
<tr>
<tr>
</tbody>
</table>
Using Selenium I will ask it to record the value of a certain row and column . This will then look at the Table element I will give it (hopefully using an XPath I can get working in this case), look at the thead and record the headers of each column. In this case that I am struggling with, the fact there is an extra 'tr' at the top of this table gets in the way of this process.
This is how the element is currently used:
[TableAlias("Detailed table")]
protected virtual IWebElement DetailedTable()
{
return Driver.FindElement(By.XPath("//table[#class='noselect']"));
}
I have tried many different ways which I can't get to work, but the gist of what I've been going for is:
//table[#class='noselect movements']/thead/tr/th[not(text()='Interest Rates')]/../../..
Here I'm stuck on going to the 'tr' element, telling it not to use it then backing out, but that selects it back again - and even that doesn't unselect the whole 'tr' element. It doesn't seem to help (to me) that the 'tr' element I'm trying to remove is blank with no class or defining features.
Is there a way of selecting the entire table except for the first 'tr' element in 'thead' as one element?
combine two xpathes. The 1st xpath take thead without the 1st tr and the 2nd tbody
//table/thead/tr[not(position()=1)] | //table/tbody
In your function that processes the THEAD tag, get the collection of TRs and start with [1] (skipping [0]) and process the rest.

How to get xpath from "Page Should Contain" in a web page using RobotFramework and Selenium

I'm writing test cases for a webpage that has a table as the main focus of the page. I'm checking that the page contains a new entry via the Page Should Contain fucntion, I then want to find the xpath for this element. I am doing this because each entry is added to the end of the table so I cannot set a static xpath for it because I don't know how many entries will be added between each run of this particular test.
Thanks in advance.
<table class="fw-table">
<thead>
<tr class="affix-header">
<th class="columnwidth-description">Email</th>
<th>Account</th>
<th>Signed up</th>
<th>Number of invalid attempts</th>
</tr>
</thead>
<tbody>
<tr>
<td>devtest</td>
<td>Account</td>
<td>true</td>
<td>0</td>
</tr>
<tr>
<td>test#testing.com</td>
<td>Account</td>
<td>true</td>
<td>1</td>
</tr>
search for: xpath=//body/div[2]/div/div[2]/div[2]/div/div/table/tbody/tr[1]/td[1]
If you need to verify that last table row contains certain text you can use the Page Should Contain Element keyword and provide an xpath locator which will find the last table row containing your text:
Page Should Contain Element xpath=//table[#class="fw-table"]//tr[last()]//td[contains(text(),'bottomline')]

selenium IDE - how to storeText just the id within a link?

How can I create a variable that has the id from one of my records on the index page?
I can use xpath to get at the anchor I want with:
storeText
//table[#id='districts']//tr/td/a[text()='Fairview Union']
id_of_another_district
but that gets the link name. I want to id for that record, i.e. in the original source code I have:
<tr>
<td>
Fairview Union
<span class='small'>(Select)</span>
</td>
<td></td>
<td></td>
<td></td>
<td class='rightalign'>01/09/2012</td>
<td></td>
<td></td>
<td><img alt="Delete" src="/assets/icons/delete-a77f3c3c125b0817085648f284c98938.png" /></td>
</tr>
and what I want to get and store in a variable in selenium is the record number 31
Using the 'store' command, you can execute javascript code in the 'target' column...
store | javascript{...} | myVar
where the ... is, substitute it with the js you need to return the id.
[Edit from Michael]
I ended up using:
storeAttribute | //*[#class='small']/a[text()='(Select)']/#href | hrefAttr
store | javascript{storedVars['hrefAttr'].replace(/.*\//,'')} | myVar
echo | ${myVar}
myVar now has the ID and I can do things like:
open
/users/change_district/${id_of_another_district}
The answer of sircapsalot (edited by Michael Durrant) is ok, but it's a bit different in my environment, regarding how the javascript is executed: not with javascript{...} So, in my case it looks like this and it works fine:
<tr>
<td>storeAttribute</td>
<td>//html/body/table/tbody/tr[3]/a#href</td>
<td>hrefAttr</td>
</tr>
<tr>
<td>storeEval</td>
<td>storedVars['hrefAttr'].replace(/.*=/,'')</td>
<td>transportIdVar</td>
</tr>
<tr>
<td>echo</td>
<td>${transportIdVar}</td>
<td></td>
</tr>
The href in my case looks like this:
http://example/local/IntegrationPick?partyId=8080595

xpath - how to find a delete link in a given table row?

Given the HTML below how can I find the delete link?
I've been able to select the row with:
//td[contains(text(),'DeleteMe')]/../../tr
but I can't seem to find a way to select the delete button.
For example I tried:
//td[contains(text(),'DeleteMe')]/../../tr//a[#class='delete_link'] but it did not select the link.
I can't use the ID's 403, 113, 112, etc and I need a script without actual ID's for re-testing purposes.
<tr class="odd" id="activity_403">
<td class="name">DeleteMe</td>
<td class="direct">false</td>
<td class="">
<ul>
<li>Language Therapist</li>
<li>Speech Therapist</li>
</ul>
</td>
<td class="">
View
Edit
Delete
</td>
</tr>
Found it:
//td[contains(text(),'DeleteMe')]/../../tr//a[contains(#class,'delete_link')]