Automate an application through selenium where id changes dynamically.how can i handle this.Pls help me..
HTML code is:-
<table border="0" cellpadding="0" cellspacing="0" width="1000px">
<tbody><tr id="ctl00_ctl00_MainContent_CarQuoteMainContent_rpQuotes_trSelectedQuote_0">
<td align="center" valign="middle" width="12%">
<input id="ctl00_ctl00_MainContent_CarQuoteMainContent_rpQuotes_chkCompare_0" name="ctl00$ctl00$MainContent$CarQuoteMainContent$rpQuotes$ctl00$chkCompare" type="checkbox">
</td>
In both the cases ( and ), I assume that the first part of the ID is unique. So you can use something like this.
//tr[contains(#id,'ctl00_')] and for input field //input[contains(#id,'ctl00_')].
Those don't look like dynamic IDs, but rather non-content-specific row IDs for elements in a list.
If that's the case, you can't immediately ascertain 'This row element is displaying data for MyCarQuotes.com' from this information alone as there's nothing in the HTML shown to base that query on.
If there's something in the rows you can use to 'identify' the content (eg a company name) - and you have a specific 'thing' you want to interact with - you could encapsulate the lookup and do something like
CheckboxForQuoteFromCompany("MyCarQuotes.com").Click();
If you're able to post more of the HTML (at least a full row), and more importantly the intention of your test, we may be able to be of more help.
Related
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.
Here there are a lot of posts for testing dropdown lists which are created using the select and option tags. But even after lot of effort, I could not find even a single post for testing dropdowns created using input tag.
html code :
<input id="CEPJICNK.ProcMgmtView.InstanceAdvancedQueryHitLimitDropDownBK" autocomplete="off" value="10" readonly="true" ct="CB" lsdata="{7:'SVSDK.com.sap.dictionary.string_530_',8:'10',10:'10',12:true}" lsevents="{Select:[{'ClientAction':'submit'},{'urEventName':'COMBOBOXSELECTIONCHANGE'}],Change:[{'ClientAction':'submit'},{'urEventName':'COMBOBOXSELECTIONCHANGE'}]}" tabindex="0" ti="0" class="urEdf2TxtRadius urEdf2TxtEnbl lsEdf3TxtHlpBtn lsEdfLeftBrdRadius lsEdFieldFocus" style="width:100%;"></td><td class="lsTblEdf3HlpBtnTd"><input type="text" readonly="true" id="CEPJICNK.ProcMgmtView.InstanceAdvancedQueryHitLimitDropDownBK-btn" tabindex="-1" ti="-1" class="urBorderBox lsEdf2HlpRadius lsEdf3HlpBtn lsEdf3HlpBtnCoB lsEdf3HlpBtnFocus"></td></tr></tbody></table>
*//Current Values in dropdown are : 10,20,30,40,All*
<tbody><tr ct="ILBI" lsdata="{0:'10',4:'10'}" id="SVSDK.com.sap.dictionary.string_530_-key-0" class="urIlb2ISel"><td class="urIlb2I urColorTxtStandard">10</td>
<td> </td></tr><tr ct="ILBI" lsdata="{0:'20',4:'20'}" id="SVSDK.com.sap.dictionary.string_530_-key-1" class="">
<td class="urIlb2I urColorTxtStandard">20</td>
<td> </td></tr><tr ct="ILBI" lsdata="{0:'30',4:'30'}" id="SVSDK.com.sap.dictionary.string_530_-key-2" class=""><td class="urIlb2I urColorTxtStandard">30</td>
<td> </td></tr><tr ct="ILBI" lsdata="{0:'40',4:'40'}" id="SVSDK.com.sap.dictionary.string_530_-key-3" class=""><td class="urIlb2I urColorTxtStandard">40</td>
<td> </td></tr><tr ct="ILBI" lsdata="{0:'-1',4:'All'}" id="SVSDK.com.sap.dictionary.string_530_-key-4" class=""><td class="urIlb2I urColorTxtStandard">All</td>
<td> </td></tr></tbody></table></div>
Hi #Sumitbit2005 What approach i found is :
1. first get hold of input tag using xpath and its id
2. get lsdata attribute of this element
3. now using string operations, extract the number part
4. now that we have the number, we can access the table by giving full xpath
5. But alas the table is in a <div> tag which is in quotes. Hence the DOM can't access it directly
6. So what I do next is first access the parent element of <div> and then access its innerHTML
7. Finally we need to verify if all the expected values exist in this innerHTML
8. A clean way to do this is to treat this innerHTML as XML and
a. calculated count of values using xpath query
b. check each value using xpath query
We have the same problem. The best approach is to click on the drop down input button (-btn) and have the input box being visible. Than you can find the table in the dom and walk through it programmatically knowing exactly how and what is in it.
You can navigate the combo box by using sendKeys(Keys.ARROW_UP / DOWN) directly but you should always have the table (drop down) visible and use it for moving up and down. Having it not open and just use the input for up down will result in stale references since it appears that the input element is rewritten to the dom every 100 or so ms (no idea whats the js is causing it). Even if we refresh the element (by refinding it) the table starts to do weired stuff even causing it to have no value at all (no value attribute at the given time) or the table starts on first index again even if we were in the middle of the options.
So always have the combo box table visible.
I have a HTML table which initially displays all records in the DB. I have just added a JQuery Datepicker component, and now I want to be able to retrieve records based on date. I have no problem extracting the date from the Datepicker component, but I do have a problem updating the existing HTML table. My code is pretty standard.
<sql:query var="retrieveSecurity" dataSource="jdbc/SecurityApp">SELECT * FROM security</sql:query>
<tbody>
<c:forEach var="row" items="${retrieveSecurity.rows}">
<tr>
<td>${row.security_id}</td>
<td>${row.description}</td>
<td class="center">${row.bid_price}</td>
<td class="center">${row.bid_percent_change}</td>
<td>${row.comment}</td>
</tr>
</c:forEach>
</tbody>
What I basically want to do is to have another SQL query, something like this...
<sql:query var="retrieveSecurity" dataSource="jdbc/SecurityApp">SELECT * FROM security where trade_date = "MYVALUE"
</sql:query>
I then want to be able to update the the tbody tag to point to this new SQL query, and call it instead. The columns won't change, so it should be the same table, just with filtered data. Not sure if what I am asking is the easiest/best solution, just looking for something that will work quickly for a prototype.
Hope this makes sense.
I has a page as follow:
<table>
<tr>
<th>Company Name</th>
</tr>
<tr>
<td> What Ever Company</td>
</tr>
</table>
The company name is placed arbitrary in the table, so I can only use the link's text to locate the link:
selenium.click("link='What Ever Company'");
However, it says: ERROR:Element link='What Ever Company' not found.
What is the problem here? Is there any other way to click on the link?
Many thanks.
EDIT
Seem that the problem is I have several links with the same text (my bad). After making the link's text unique, I use selenium.click("//a[contains(text(),'Test Campaign 1756237989')]") and it works.
Could this be because you're forgetting the space at the start of the link?
selenium.click("link=' What Ever Company'");
^
Another possible way of clicking the link, is to use an XPath expression:
selenium.click("//a[contains(.,'What Ever Company')]");
This will match all links with 'What Ever Company' in it.
If you want it more exact:
selenium.click("//a[.=' What Ever Company']");
This will only match if the anchor equals ' What Ever Company'.
Another option is to make the search more specific (i.e. tell the locator this link is always inside a <td> with an <a> inside):
selenium.click("//td[a]/a[contains(.,'What Ever Company')]");
The //td[a] looks for all <td> elements with <a> inside. (Differs from //td/a in that if you look for elements with //td[a][2] you get the second <a> which is inside a <td>, while //td/a[2] on the other hand gets the second <a> of the first <td>.)
EDIT: I thought using . as a reference to text() in the XPath expressions should work, but if it doesn't, try using text() instead.
Try these XPaths:
"//table/tr[2]/td/a"
or
"//a[contains(text(), 'What Ever Company')]"
Should work.
I'm using Struts2 iterator to setup a list of checkbox in a table. I want to have 10 checkbox per row, so I'm doing the following:
<table>
<tr>
<s:iterator value="securityMasterFields" status="fieldNameStatus" var="fieldName">
<s:if test="#fieldNameStatus.index % 10 ==0">
</tr><tr>
</s:if>
<td>
<s:checkbox name="fieldsToShow" fieldValue="%{fieldName}" value="%{fieldName}"/>
</td>
</s:iterator>
</tr>
</table>
It never goes through the if, so I'm assuming the mod is not been calculated correctly. How do I do it?
thanks
Well, I had to add some parentheses and it worked correctly. The loop was working, it was just that it wasn't going through the if.
<s:if test="(#fieldNameStatus.index % 8 )==0"></tr><tr></s:if>
It looks good to me. Two thoughts:
1) try printing the result of the test in s:property tag
2) It looks like you will have empty table rows... Are you looking at the generated html or just the output, because if it is just the output then unless you have some CSS giving you some table padding and borders, without an empty 'td' element the row might collapse and make it appear as if nothing is being added. So do make sure you print the empty 'td' elements too!