How to get xpath from "Page Should Contain" in a web page using RobotFramework and Selenium - 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')]

Related

check text is present in table (selenium IDE)

I am trying to check that the text "AUTOTEST" is present somewhere in my HTML table "tableTest".
In selenium IDE, i put this code :
<tr>
<td>verifyTextPresent</td>
<td>//table[#id='tableTest']</td>
<td>AUTOTEST</td>
</tr>
Here is the code of the HTML table :
<table id="myTable">
<tr>
<td>AUTOTEST</td>
<td>TEST</td>
</tr>
<tr>
<td>UOI</td>
<td>TEST</td>
</tr>
</table>
But the assertion always return false whereas text is well present in the table.
Which command can i use to check that text is present in my table ?
I think the reason for this is that you're trying to verify one bit of text using the entire table as a locator, rather than the specific section that contains the text you're looking for, as a result it isn't matching because you have other content within the table, and your verify step is looking for an exact match. You'd need to put in wildcards around AUTOTEST in your selenium script, that way it will just look to make sure that it is present somewhere within the table.
Script and log testing against the HTML code you provided for the table.
<tr>
<td>verifyText</td>
<td>css=table</td>
<td>*AUTOTEST*</td>
</tr>
[info] Playing test case Untitled
[info] Executing: |verifyText | css=table | *AUTOTEST* |
[info] Test case passed
VerifyTextPresent is depracted, VerifyText will do what you want:
<tr>
<td>verifyText</td>
<td>//table[#id='myTable']</td>
<td>*AUTOTEST*</td>
</tr>
The other thing to note is the text is the FULL text, so in this case you need to use a glob and use the * which serves as a wildcard.
Note: by the way you could also just specify the id (and not the full xpath) like this:
<tr>
<td>verifyText</td>
<td>myTable</td>
<td>*AUTOTEST*</td>
</tr>

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.

storeAttribute or storeElementPresent?

I am hoping I can get some help, I need to know how to accomplish something but not sure how as I am new to selenium commands.
I tried the following:
storeEval
javascript{window.document.getElementById('myDiv');}
myResult
then
gotoIf
${myResult} == false
lblWhereToGoIfStyleDisplayIsNotVisible
This does not give me what I want as I always have the div in my page, even if the style = display : none.
What I need is to get the display property of the style of the div and check that value to see if none to then jump to the lblWhereToGoIfStyleDisplayIsNotVisible label
I was thinking I need to use storeAttribute or storeElementPresent but not sure how to implement properly to get result needed.
Any help is appreciated, thanks in advance.
I finally got my script working on my own as I was getting no quick responses, I tried many other type of searches about the gotoIf, css attributes, etc... and found one link at
http://sysmagazine.com/posts/190358/
which helped alot...
here is the final code if someone is interested in knowing how to get a div, display property value, store it, compare it to == NONE (if div is hidden) and go to the next label (test location in the script) in order to continue testing (setting dropdown values)....
this allows me to set values only if the div exists and is visible meaning the dropdown will have selections
This allowed me also to define 1 script for multiple possible test cases, just by changing the url based on the test I want to have.
Cheers!
<tr>
<td>pause</td>
<td>2000</td>
<td></td>
</tr>
<tr>
<td>storeEval</td>
<td>window.getComputedStyle(window.document.getElementById('trProtection'),null).getPropertyValue('display');</td>
<td>myResult</td>
</tr>
<tr>
<td>echo</td>
<td>javascript{storedVars['myResult'];}</td>
<td></td>
</tr>
<tr>
<td>gotoIf</td>
<td>storedVars['myResult'] == "none"</td>
<td>lblDivision</td>
</tr>
<tr>
<td>select</td>
<td>id=selProtection</td>
<td>label=DISABILITY</td>
</tr>
<tr>
<td>label</td>
<td>lblDivision</td>
<td></td>
</tr>
<tr>
<td>pause</td>
<td>2000</td>
<td></td>
</tr>

How to get an integer on selenium IDE

I got this page where I want to run my code:
I got a css with this properties:
<center>
Radicacion Exitosa, Numero Radicacion: 12 - 132263…
</center>
I want to store the "132263" in a variable because I have to copy and paste them in another window, so I want to know if there is a way to only get the number from that tag...specially the "132263" because if I store a Variable Selenium IDE only will get the full text.
Thanks
Using regex in javascript will do this for you:
<tr>
<td>storeText</td>
<td>//center</td>
<td>full</td>
</tr>
<tr>
<td>storeEval</td>
<td>var regex=/\-\s+(\d+)\s+\-/;regex.exec(storedVars['full'])[1];</td>
<td>number</td>
</tr>
<tr>
<td>echo</td>
<td>${number}</td>
<td></td>
</tr>
We're capturing the text from the CENTER tag and then running the regex to grab the number out of it (using the group (\d+) and returning [1]).

Clicking other cell's child element in Selenium WebDriver Java

What is the code to be used to click the "a" element by looking first for the "td" that contains certain texts?
<table>
<tbody>
<tr>
<td><a class="link">link</a></td>
<td>1st</td>
</tr>
<tr>
<td><a class="link">link</a></td>
<td>2nd</td>
</tr>
</tbody>
</table>
I used this code but it doesn't work.
driver.findElement(By.xpath("//td[contains(text(), '1st')]/following-sibling::a[#class='link']")).click();
Try this xPath //td[contains(text(), '1st')]/../td/a[#class='link'].
Your mistake comes from the axis "following-sibling". The tag a is not a sibling of the td tag. I use .. to go up, then select td again and then a.
I tested the xPath using http://www.xpathtester.com/xpath. It works. Beware that every browser comes with their own xPath implementation, therefore it could behave a bit differently.
However, as there is nothing exotic in the xPath expression, I don't expect any problem.