How to get span element into table row (Selenium Webdriver) - java

I'm new to Selenium webdriver. Can someone help me how to get the span element in table row
<span class="small-info" title="zim.lu#en.com , stins.gib#en.com "> zim.lu#en.com , stin.gib#en.com </span>
in below Table
<table class="k-selectable" role="grid" data-role="selectable">
<colgroup>
<tbody role="rowgroup">
<tr class="k-state-selected" role="row" data-uid="39c56242-2108-4b6d-b80f-1e2f266cd02f" aria-selected="true">
<td role="gridcell">
<div class="left-info">
<div id="item193689" class="inbox-info">
<div class="left-inboxInfo">
<h2 class="SubjecthOverflow">
<span class="small-info" title="zim.lu#en.com , stins.gib#en.com "> zim.lu#en.com , stin.gib#en.com </span>
<div id="policydiv193689">
</div>
<div class="right-inboxInfo">
</div>
</td>
</tr>
<tr class="k-alt" role="row" data-uid="32a122c7-2e7b-4a28-bb77-5fde6679e6ec">
<td role="gridcell">
<div class="left-info">
<div id="item202147" class="inbox-info">
<div class="left-inboxInfo">
<h2 class="SubjecthOverflow">
<span class="small-info" title="kev.kind#en.com , vin.kami#en.com "> ke.kin#en.com , vi.kami#en.com </span>
<div id="policydiv202147">
</div>
<div class="right-inboxInfo">
</div>
</td>
</tr>
</tbody>
</table>
I tried this code
WebElement table_element = dr.findElement(By.className("k-selectable"));
List<WebElement>tr_collection=table_element.findElements(By.xpath("//span[#class='small-info']"));
System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tr_collection.size());
Output not showing

List<WebElement>tr_collection=dr.findElements(By.xpath("//span[#class='small-info']"));
System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tr_collection.size());

You can find the element by using xpath or css selector
Try this below code in your list
dr.findElements(By.xpath("//*[#role='grid']/colgroup/tbody/tr/td/div/div/div/h2/span"));

Related

FreeMarker grab variable from list when is in loop that is selected from table

I am trying to get a variable department.id for the selected department when anchor edit or delete is clicked I try with <#assgn departmentId = "${department.id}"> but that return the last id from the table.Since is in the loop I also try with onclick="<#assgn departmentId = "${department.id}">" this also return me last id is there a way to get the current id of the department when is clicked edit or delete with Apache Free Marker
HTML
<tbody>
<#assign count = 1>
<#list departmentsList as department>
<tr>
<td>${count}</td>
<td>${department.name}</td>
<td class="text-right">
<div class="dropdown dropdown-action">
<i class="material-icons">more_vert</i>
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#edit_department"><i class="fa fa-pencil m-r-5"></i> Edit</a>
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#delete_department"><i class="fa fa-trash-o m-r-5"></i> Delete</a>
</div>
</div>
</td>
</tr>
<#assign count ++>
</#list>
</tbody>
<tbody>
<#assign count = 1>
<#list departmentsList as department>
<tr>
<td>${count}</td>
<td>${department.name}</td>
<td class="text-right">
<div class="dropdown dropdown-action">
<i class="material-icons">more_vert</i>
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#edit_department" onclick="myEditFunction(${department.id})"><i class="fa fa-pencil m-r-5"></i> Edit</a>
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#delete_department" onclick="myDeleteFunction(${department.id})"><i class="fa fa-trash-o m-r-5"></i> Delete</a>
</div>
</div>
</td>
</tr>
<#assign count ++>
</#list>
</tbody>
<script>
function myDeleteFunction(departmentId) {
console.log("deleting department " + departmentId);
}
function myEditFunction(departmentId) {
console.log("editing department " + departmentId);
}
</script>

Making a hidden element displayable and clickable in selenium java

I have the following element:
<table class="dijit " data-dojo-attach-point="_buttonNode" cellspacing="0" cellpadding="0" role="lbox" aria-haspopup="true" tabindex="0" id="POS_domain" data-id="domain" widgetid="POS_domain" aria-expanded="false" aria- invalid="false" style="user-select: none;" popupactive="true" aria-owns="POS_domain">
<tbody role="presentation">
<tr role="presentation">
<td class="dijitReset" role="presentation">
<div class="dijitReset Text" data-dojo-attach-point="container" role="presentation">
<span role="option" aria-selected="true" class="dijitLabel ">adrija</span>
</div>
<div class="dijitContainer">
<input class="dijitInner" value="Χ " type="text" tabindex="-1" readonly="readonly" role="presentation">
</div>
<input type="hidden" data-dojo-attach-point="vn" value="adrija" hidden="true">
</td>
<td class="dijitArrowButtonContainer" data-dojo-attach-point="titleNode" role="presentation">
<input class="dijitInner" value="▼ " type="text" tabindex="-1" readonly="readonly" role="presentation">
</td>
</tr>
</tbody>
</table>
The above element is an element of dropdown and is hidden. The code that I have written is:
private WebElement domainDropdown = Driver.driver.findElement(By.id("POS_domain"));
domainDropdpwn.click();
private WebElement adrija = Driver.driver.findElement(By.xpath("//input[#value='adrija' and #data-dojo-attach-point='vn']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", adrija);
It says it's not able to find the element.
Please help. Thanks. :)
The desired <input> tag is having the attributes type="hidden" and hidden="true", so to click() on the element you can use the following solution:
//driver being an instance of WebDriver
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//table[#class='dijit ' and #id='POS_domain']"))).click();
WebElement my_adrija = driver.findElement(By.xpath("//input[#value='adrija' and #data-dojo-attach-point='vn']"));
((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('hidden')", my_adrija)
((JavascriptExecutor)driver).executeScript("arguments[0].setAttribute('type','text')", my_adrija)
WebElement my_new_adrija = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#value='adrija' and #data-dojo-attach-point='vn']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", my_new_adrija);

how to extract the href value using selenium in java

here i want to extract the href value from below code,
<table id="offers_table" class="fixed offers breakword" summary="" width="100%" cellspacing="0">
<tbody>
<tr>
<tr>
<tr>
<td class="offer onclick ">
<table class="fixed breakword ad_id1ezENl" summary="Ad" data-photos="2" width="100%" cellspacing="0">
<tbody>
<tr>
<td rowspan="2" width="164">
<div class="space">
<span class="rel inlblk detailcloudbox">
<a class="thumb vtop inlblk rel tdnone linkWithHash scale5 detailsLink"
href="https://www.olx.in/item/hyundai-accent-car-ID1ezENl.html#1a86c09693" title="">
</span>
</div>
</td>
<td valign="top">
<td class="wwnormal tright td-price" width="170" valign="top">
</tr>
<tr>
</tbody>
</table>
I have tried this below code but it shows the error
WebElement ele=driver.findElement(By.id("offers_table"));
WebElement href=ele.findElement(By.xpath("//tr[3]/span[#class='rel inlblk detailcloudbox']/a[#href]"));
System.out.println(href.getAttribute("href"));
You can use cssSelector insted of xpath for this case, try using below code:
String hrefvalue = driver.findElement(By.cssSelector("span.rel.inlblk.detailcloudbox > a")).getAttribute("href");
Try this xpath:
//a[#class='thumb vtop inlblk rel tdnone linkWithHash scale5 detailsLink']
[#href='https://www.olx.in/item/hyundai-accent-car-ID1ezENl.html#1a86c09693']

Selecting a check box from a list of check boxes

In one scenario, I have to select a Check box with text "ALL".
Here is the sample HTML code:
<tbody id="modulePermissionDatagridId_data" class="ui-datatable-data ui-widget-content">
<tr class="ui-widget-content ui-datatable-even" role="row" data-ri="0">
<tr class="ui-widget-content ui-datatable-odd" role="row" data-ri="1">
<td role="gridcell">
<div id="modulePermissionDatagridId:1:j_idt124" class="ui-chkbox ui-widget multipleSelectionChkBox">
<div class="ui-helper-hidden-accessible">
<input id="modulePermissionDatagridId:1:j_idt124_input" type="checkbox" onchange="handleChkBoxValueChange(this)" name="modulePermissionDatagridId:1:j_idt124_input"/>
</div>
**<div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default">**
<span class="ui-chkbox-icon ui-c"/>
</div>
<span class="ui-chkbox-label">ALL</span>
</div>
</td>
</tr>
Note: In the above HTML, the check box is pointing to the 'div' element above 'span'.
Currently i am using the following code which is working fine:
WebElement perm = driver.findElement(By.id("modulePermissionDatagridId_data"));
List<WebElement> permno = perm.findElements(By.tagName("tr"));
int i=0;
for(WebElement wb : permno)
{
String permName = wb.getText();
System.out.println(permName);
//for (int i=0; i< permno.size(); i++)
//{
System.out.println(i);
if (permName.equals("ALL"))
{
System.out.println(permName);
System.out.println(i);
driver.findElement(By.xpath("//*[#id='modulePermissionDatagridId:"+i+":j_idt124']/div[2]")).click();
//break;
}
//}
i++;
}
But i want to simplify this code. So i tried the following XPATH which is not working:
driver.findElement(By.xpath("//tbody[#id='modulePermissionDatagridId_data']/tr[./td/div[span='ALL']]/div")).click();
Any suggestions please?
This xpath worked for me:
//span[contains(text(), 'ALL') and #class='ui-chkbox-label']/../div/input

how to verify the sorting in collapse group selenium web driver-java

i want to test the sorting in collapse group?? it's possible or not? Please share that how to do it. blow pic have two group 1) Branch:Clifton and 2) Branch: Holsopple.(columns are sorted by clicking on Columns heading(Contact, Type etc)).below mentioned code work where group is not exist but fail where is group on page.
when i compare the gettext in java it shows result false while sorting of the text on the page is correct,because my java code gets the text in whole column and sorting is on collapse group. I wanna write the code which verify the sorting of columns on collapse group base.
HTML is here:
<tbody>
<tr class="rgGroupHeader">
<td class="rgGroupCol">
<td colspan="9">
<p>Branch: Clifton</p>
</td>
</tr>
<td class="rgGroupCol"/>
<td style="display:none;" title="289855">289855</td>
<td style="display:none;" title="31">31</td>
<td style="display:none;"/>
<td style="display:none;" title="12">12</td>
<td style="display:none;" title="6">6</td>
<td class="col_priority">
<td title="10/24/2013">10/24/2013</td>
<td class="col_status">
<div id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl19_divStatus" class="status_active" title="Open - Active"/>
</td>
<td>
<td>
<td>
<td title="Nawaz, S (10/22/2013)">
<td class="col_manager_instruction">
<td class="col_expiry" title="N/A">N/A</td>
</tr>
<tr id="ctl00_CPHPageContents_dtgLeads_ctl00__8" class="rgRow">
<td class="rgGroupCol"/>
<td style="display:none;" title="289856">289856</td>
<td style="display:none;" title="31">31</td>
<td style="display:none;"/>
<td style="display:none;" title="11">11</td>
<td style="display:none;" title="6">6</td>
<td class="col_priority">
<td title="10/24/2013">10/24/2013</td>
<td class="col_status">
<td>
<td>
<td>
<td title="Nawaz, S (10/22/2013)">
<td class="col_manager_instruction">
<td class="col_expiry" title="11/25/2013">11/25/2013</td>
</tr>
<tr class="rgGroupHeader">
<td class="rgGroupCol">
<input id="ctl00_CPHPageContents_dtgLeads_ctl00__35__0" class="rgCollapse" type="button" title="Collapse group" onclick="$find("ctl00_CPHPageContents_dtgLeads_ctl00")._toggleGroupsExpand(this, event); return false;__doPostBack('ctl00$CPHPageContents$dtgLeads$ctl00$ctl37$ctl00','')" value=" " name="ctl00$CPHPageContents$dtgLeads$ctl00$ctl37$ctl00"/>
</td>
<td colspan="9">
<p>Branch: Holsopple</p>
</td>
</tr>
<tr id="ctl00_CPHPageContents_dtgLeads_ctl00__16" class="rgRow">
<td class="rgGroupCol"/>
<td style="display:none;" title="289768">289768</td>
<td style="display:none;" title="2">2</td>
<td style="display:none;"/>
<td style="display:none;" title="12">12</td>
<td style="display:none;" title="4">4</td>
<td class="col_priority">
<div id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_divPriority" class="priority_high" title="High"/>
</td>
<td title="06/27/2013">06/27/2013</td>
<td class="col_status">
<div id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_divStatus" class="status_active" title="Open - Active"/>
</td>
<td>
<div id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_divInner">
<a id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_hlnkContact" href="/Leads/Research/289768">John Ross</a>
<input id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_hdfContactID" type="hidden" value="174120" name="ctl00$CPHPageContents$dtgLeads$ctl00$ctl38$hdfContactID"/>
<div id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_divContactCardControl" class="pos_r"/>
</div>
</td>
<td>
<div class="lead_type">
<a id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_lnkType" class="lead_type_link" href="/Leads/Research/289768" title="Maturing CD 100">Maturing CD 100</a>
<a id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_lnkDownArrow" class="down_arrow" onclick="showCloseTransferLayer('ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_CloseTransferLayer')" href="javascript:;"/>
<span class="pos_r">
<div id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_CloseTransferLayer">
<a id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_lnkCloseLead" onclick="return ShowPopupForm('/Forms/Popups/CloseLead.aspx?LeadID=289768','WindowCloseLead');" href="javascript:;">Cancel Lead</a>
<a id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_lnkTransferLead" onclick="return ShowPopupForm('/Forms/Popups/TransferLead.aspx?LeadID=289768','WindowTransferLead');" href="javascript:;">Transfer Lead</a>
</div>
</span>
</div>
</td>
<td>
<div id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_divAssignedTo">
<div id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_ddlAssignedTo" class="RadComboBox RadComboBox_Default assigned_to_combo" style="width:160px;">
<table style="border-width: 0px; border-collapse: collapse;" summary="combobox">
<tbody>
<tr class="rcbReadOnly">
<td class="rcbInputCell rcbInputCellLeft" style="width:100%;">
<input id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_ddlAssignedTo_Input" class="rcbInput radPreventDecorate" type="text" readonly="readonly" value="Org, T" name="ctl00$CPHPageContents$dtgLeads$ctl00$ctl38$ddlAssignedTo" autocomplete="off"/>
</td>
<td class="rcbArrowCell rcbArrowCellRight">
<a id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_ddlAssignedTo_Arrow" style="overflow: hidden;display: block;position: relative;outline: none;">select</a>
</td>
</tr>
</tbody>
</table>
<div class="rcbSlide" style="z-index:6000;">
<div id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_ddlAssignedTo_DropDown" class="RadComboBoxDropDown RadComboBoxDropDown_Default " style="display:none;">
<div class="rcbScroll rcbWidth" style="width:100%;">
<ul class="rcbList" style="list-style:none;margin:0;padding:0;zoom:1;">
<li class="rcbItem">Ghaffar, A</li>
<li class="rcbItem">Keller, K</li>
<li class="rcbItem">Nawaz, S</li>
<li class="rcbItem">Org, 1</li>
<li class="rcbItem">Org, T</li>
</ul>
</div>
</div>
</div>
<input id="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_ddlAssignedTo_ClientState" type="hidden" name="ctl00_CPHPageContents_dtgLeads_ctl00_ctl38_ddlAssignedTo_ClientState" autocomplete="off"/>
</div>
</div>
</td>
<td/>
<td class="col_manager_instruction">
<td class="col_expiry" title="N/A">N/A</td>
</tr>
</tbody>
Java Code:
List<String> displayedNames = new ArrayList<String>();
List<String> SortedNames = new ArrayList<String>();
String getData;
Thread.sleep(thread);
for(int i=0;i<tableType.size();i++)
{
getData=tableType.get(i).getText();
System.out.println(getData);
displayedNames.add(getData);
SortedNames.add(getData);
}
System.out.println(displayedNames);
Thread.sleep(thread);
List<String> sortingOperation = displayedNames;
Thread.sleep(thread);
Collections.sort(sortingOperation);
Thread.sleep(thread);
Assert.assertEquals(SortedNames, sortingOperation);
List<String> displayedNames = new ArrayList<String>();
List<String> SortedNames = new ArrayList<String>();
Thread.sleep(10000);
WebElementtableType=action.driver.findElement(By.xpath("//[#id='table_1_core_table_content']/tbody/tr"));
Thread.sleep(10000);
List<WebElement>rowElmt=tableType.findElements(By.xpath("//tr/td[5]"));
String getData;
Thread.sleep(5000);
for(int i=2;i<rowElmt.size();i++)
{
getData=rowElmt.get(i).getText();
displayedNames.add(getData);
SortedNames.add(getData);
}
System.out.println(displayedNames);
Thread.sleep(5000);
List<String> sortingOperation = displayedNames;
Collections.sort(sortingOperation);
Assert.assertEquals(SortedNames, sortingOperation);
}

Categories

Resources