Selecting a check box from a list of check boxes - java

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

Related

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);

Finding previous sibling element using selenium xpath dynamically

I am writing selenium scripts for the following code.
<div id="abc" class="ui-selectmanycheckbox ui-widget hpsapf-chechkbox">
<div class="ui-chkbox ui-widget">
<div class="ui-helper-hidden-accessible">
<input id="abc:0" name="abc" type="checkbox" value="0" checked="checked">
</div>
<div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default ui-state-active">
<span class="ui-chkbox-icon ui-icon ui-icon-check ui-c"></span>
</div>
</div>
<span class="hpsapf-radio-label">
<label for="abc:0">Herr</label>
</span>
<div class="ui-chkbox ui-widget">
<div class="ui-helper-hidden-accessible">
<input id="abc:1" name="abc" type="checkbox" value="1">
</div>
<div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default">
<span class="ui-chkbox-icon ui-icon ui-icon-blank ui-c"></span>
</div>
</div>
<span class="hpsapf-radio-label">
<label for="abc:1">Frau</label>
</span>
</div>
These are the checkbox like the following.The number of the checkboxes are changed as per database values.
In my code i am first checking whether the "Frau" check box is selected or not. so i tried following snippet.
WebElement mainElement= driver.findElement(By.id("abc"));
WebElement label=mainElement.findElement(By.xpath(".//label[contains(#for,'abc')][text() = 'Frau']"));
WebElement parent = label.findElement(By.xpath(".."));
WebElement div = parent.findElement(By.xpath("preceding-sibling::::div"));
WebElement checkBox = div.findElement(By.className("ui-chkbox-box"));
String css = checkBox.getAttribute("class");
if(css.contains("ui-state-active")) {
return "checked";
}
else
{
return "unchecked";
}
But when i tried to execute this script. WebElement div = parent.findElement(By.xpath("preceding-sibling::::div")); gives me the first div tag and not the preceding one. I want a preceding sibling.
Use :: and index, not ::::
WebElement div = parent.findElement(By.xpath("preceding-sibling::div[1]"));

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

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"));

Getting Input tag id, value in webdriver using java

Html
<table id="tblRenewalList" class="adminlist dataTable" width="100%" cellspacing="1" cellpadding="1" border="1" style="margin-left: 0px; width: 100%;" aria-describedby="tblRenewalList_info">
<thead>
</thead>
<tbody role="alert" aria-live="polite" aria-relevant="all">
<tr class="odd">
<td class="alignCenter">
<input id="chkRenewal_868" class="chkPatent" type="checkbox" onclick="RenewalSelection(this)" companyid="33" value="868">
</td>
</tr>
</table>
with above Html i want to scrape the id, value
following are my java code, when i try with below code, its return empty values, please find the code
WebElement inputValues = driver.findElement(By
.xpath("//*[#id='tblRenewalList']/tbody/tr[1]/td[1]"));
String idValue = inputValues.getAttribute("id");
String ed2 = inputValues.getAttribute("value");
following are my expected output
id = chkRenewal_868
value = 868
The document isn't well-formed, i don't know if that matters for webdriver,
but XPath must be
//*[#id='tblRenewalList']/tbody/tr[1]/td[1]/input

Java Get ALL elements from a multiselect Box

I have a multi-select Box and I'm doing some javascript to sort the order of the elements in the box. I want to submit the whole array after sorting back to the Java and not just the select items. How can I achieve this?
JSP:
<script type="text/javascript">
$(document).ready(function(){
$("#mup")[0].attachEvent("onclick", moveUpItem);
$("#mdown")[0].attachEvent("onclick", moveDownItem);
});
function moveUpItem(){
$('#list option:selected').each(function(){
$(this).insertBefore($(this).prev());
});
}
function moveDownItem(){
$('#list option:selected').each(function(){
$(this).insertAfter($(this).next());
});
}
</script>
<table width="100%">
<tr>
<td width="50%" align="center">
<h1>DET Column Maintenance</h1>
</td>
</tr>
</table>
<form action="process.det_column_order" method="post" name="detColumnSortorder" >
<table class="data_table">
<tr align="center">
<td>
<select id="list" name="fieldNames" size="35" multiple="multiple" style="width: 250px;">
<c:forEach var="field" items="${detFields}">
<option value="${field.fieldName}">${field.displayName}</option>
</c:forEach>
</select>
</td>
<tr>
<td style="text-align: center;">
<button id="mup">Move Up</button>
<button id="mdown">Move Down</button>
</td>
</tr>
<tr>
<td style="text-align: center;">
<input name="action" type="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
FORM:
private String[] fieldNames;
public String[] getFieldNames() { return this.fieldNames; }
public void setFieldNames(String[] val) { this.fieldNames = val; }
Since forms only submit the selected values, you'll need a little more JS and another form field.
Introduce a hidden form field that will hold the values you care about:
<input type="hidden" name="fieldNamesOrder" id="fieldNamesOrder"/>
And after every click of Move Up/Move Down:
var order = [], sel = document.getElementById("list");
for(var i = 0, len = sel.options.length; i < len; i++) {
order.push(sel.options(i).value);
}
document.getElementById("fieldNamesOrder").value = order.join(",");
Then, on the server side, you can read your ordered field names out of that posted field.
You either need to create a hidden text field in your HTML form that stores the values from each of your options... or you need to programatically select all of the options before the form is submitted.

Categories

Resources