click xpath for a dynamic checkbox in selenium - selenium

HTML:
input type="checkbox" value="21-22-122-122" id="woWizardDevices_checkboxset_option_21-22-122-122|cb" data-oj-option-id="woWizardDevices_checkboxset_option_21-22-122-122" name="woWizardDevices_checkboxset_21-22-122-122" aria-label="[['Checkbox Select Row ' + row.item.data.id]]" class="oj-checkbox oj-component oj-enabled oj-component-initnode oj-selected" data-oj-internal="" placeholder="" data-oj-tabmod="NaN" tabindex="-1" xpath="1"
The ids keep on updating
I have tried:
List<WebElement> AllCheckboxes = driver.findElements(
By.xpath("//input[#type='checkbox' and starts-with(#id,'woWizardDevices_checkboxset_option_21-22')]"));
int size = AllCheckboxes.size();
for (int i = 0; i < size; i++) {
AllCheckboxes.get(i).click();
break;
}
but it fails.

Related

Verify 2 web elements in table are equal or not

List<WebElement> tdata=driver.findElements(By.tagName("tr"));
List<WebElement> tdata1=driver1.findElements(By.tagName("tr"));
for(int i=0,j=0; i<tdata.size() && j<tdata1.size();i++,j++ )
{
WebElement row = tdata.get(i);
WebElement row1 = tdata.get(j);
System.out.print(row1.getText());
System.out.print(row1);
if(row.getText().equals(row1.getText()))
{
System.out.println(row.getText());
}
else if(!(row.getText().equals(row1.getText())))
{
System.out.print("Not matching text");
System.out.println(row1.getText());
}
}
This is my code for comparing 2 web table, I am Unable to verify content equal or not equal. For unmatched text also it is not printing anything. else if part is not capturing if elements are not equal.
try
List<WebElement> tdata= ...
List<WebElement> tdata1= ...
int common= 0;
for (WebElement element: tdata)
if (tdata1.contains(element))
common++;

How to get data from web page without scrolling using selenium webdriver

How to get all data from the webpage without using scroll or page up and down.
int n = 10;
for (int i = 0; i < n; i++) {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#type=\"text\"]")));
List<WebElement> FV = driver.findElements(By.xpath("//*[#type=\"text\"]"));
WebElement aa = FV.get(i);
String cc = aa.getAttribute("value");
System.out.println(cc);
After I run this code it displays blank.

Array value can not initialize with 0 in selenium webdriver/ java

for loop value can not initialize with 0 in selenium webdriver/ java
String arr[] = new String[4];
for(int i = 0; i <4; i++) {
String text = driver
.findElement(By.xpath("html/body/select/option["+i+"]")).getText();
arr[i] = text;
System.out.println(arr[i]+" ");
}
This is html which i have to read all the element.
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
You can initialize the array with 0 but not in the scenario which you have specified.
You are finding an element with the xpath: html/body/select/option["+i+"]
where i is the position of the option element with respect to the select Element. It can never be zero.
If you want to start an array by zero then you should use the code specified below:
String arr[] = new String[4];
for(int i = 0; i <4; i++) {
String text = driver.findElements(By.xpath("html/body/select/option")).get(i).getText();
arr[i] = text;
System.out.println(arr[i]+" ");
}
Explanation: driver.findElements(By.xpath("html/body/select/option")) return the List of WebElement. You can get the element at index 0 which is supposed to be equivalent to "html/body/select/option[1]"
Html indexes starts from 1, not 0. The xpath should be
"html/body/select/option["+ (i + 1) +"]"
However, instead of locating the option one by one you can use Select class
WebElement dropdown = driver.findElement(By.xpath("html/body/select"));
Select select = new Select(dropdown);
List<WebElement> options = select.getOptions();
String arr[] = new String[options.size()];
for(int i = 0 ; i < options.size() ; i++) {
arr[i] = options.get(i).getText();
System.out.println(arr[i] + " ");
}

How to show the selected options from a multi select drop down using selenium java?

I'm trying to show all the selected options from a multi select drop down list. But not getting the proper way to do this. Please help me on this.
Here is the html code for the drop down:
<select multiple id="fruits">
<option value="banana">Banana</option>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
<option value="grape">Grape</option>
</select>
Here is the code i'm trying with:
public void dropDownOperations()
{
driver.get("http://output.jsbin.com/osebed/2");
Select DDLIST = new Select(driver.findElement(By.id("fruits")));
DDLIST.selectByIndex(0);
String currentvalue = DDLIST.getFirstSelectedOption().getText();
System.out.println(currentvalue);
DDLIST.selectByIndex(1);
String currentvalue1 = DDLIST.getFirstSelectedOption().getText();
System.out.println(currentvalue1);
}
I also tried with this code:
Here i'm getting this output:
[[[[[ChromeDriver: chrome on XP (69aee19e9922ca218ff47c0ccdf1bbbc)] ->
id: fruits]] -> tag name: option], [[[[ChromeDriver: chrome on XP
(69aee19e9922ca218ff47c0ccdf1bbbc)] -> id: fruits]] -> tag name:
option]]
public void dropDownOperations1()
{
driver.get("http://output.jsbin.com/osebed/2");
Select DDLIST = new Select(driver.findElement(By.id("fruits")));
DDLIST.selectByIndex(0);
DDLIST.selectByIndex(1);
List<WebElement> currentvalue1 = DDLIST.getAllSelectedOptions();
System.out.println(currentvalue1);
}
Your second approach should work fine with a minor fix. getAllSelectedOptions() will return a List of selected options as WebElement. You need to iterate over the list to get the text from WebElement.
List<WebElement> selectedOptions = DDLIST.getAllSelectedOptions();
for (WebElement option : selectedOptions){
System.out.println(option.getText());
}
Try This:
List<WebElement> allSelected = select.getAllSelectedOptions();
Iterator itr = allSelected.iterator();
while(itr.hasNext()){
WebElement item = (WebElement) itr.next();
System.out.println(item.getText());
}
Try this below code, It will select one by one options from dropdown.
Select DDLIST = new Select(driver.findElement(By.id("fruits")));
DDLIST.selectByIndex(0);
DDLIST.selectByIndex(1);
List<WebElement> selectedOptions = DDLIST.getAllSelectedOptions();
for(int i=0; i<selectedOptions.size(); i++)
{
System.out.println(DDLIST.getOptions().get(i).getText());
}
Try This:
System.out.println(DDLIST.selectByIndex(0).getText());
System.out.println(DDLIST.selectByIndex(1).getText());
And so on. Instead of using a variable and trying it.
Try this:
Select DDLIST = new Select (driver.findElement(By.id("fruits")));
for(int i=0; i<DDLIST.getOptions().size(); i++)
System.out.println(DDLIST.getOptions().get(i).getText());

YUI Datatable - Get ID of DOM Element after page has loaded and use it in other YUI events

Okay so I have a YUI Datatable. Most of it is exactly as the how to guide says to construct it.
I have an event that governs changing the rows per page. It's linked to the rows per page drop down element and it saves the value of that drop down as a cookie when the drop down is changed.
var onRPPChange1 = YAHOO.util.Event.addListener("yui-pg0-1-rpp24", "change", getRPP_1);
The problem is that "yui-pg0-1-rpp24" (the ID of the drop down) changes whenever I make updates to my data table. I would like to extend this so that when the page loads it will dynamically insert the ID of that drop down into this event listener so that I don't have to keep editing it after future updates.
I've managed to construct that following that will capture the ID and I can alert it after the table loads, but so far, including the result of this function in the above addListener code isn't working.
var setRPPVars = function() {
YAHOO.util.Event.onAvailable("rppSpan", this.handleOnAvailable, this);
}
var rppSpanIds = new Array();
var rppArray = new Array();
setRPPVars.prototype.handleOnAvailable = function() {
var spans = document.getElementsByTagName("span");
var n = 0;
for(var i=0; i<spans.length; i++){
if(spans[i].id == "rppSpan"){
rppSpanIds[n] = spans[i];
if(n == 0){
rppTopID = rppSpanIds[n].firstChild.id;
rppArray[0] = rppTopID;
}
else if(n==1){
rppBottomID = rppSpanIds[n].firstChild.id;
rppArray[1] = rppBottomID;
}
n++;
}
}
alert(rppTopID);
alert(rppBottomID);
alert(rppArray);
}
var rppEvent = new setRPPVars();
//this is the part that doesn't work:
var onRPPChange0 = YAHOO.util.Event.addListener(rppArray[0], "onchange", getRPP_0);
function getRPP_0(){setRPPVars();oRPP = rppTopID;alert("rppTopID: "+rppTopID); alert("oRPP: "+oRPP);};
Any suggestions you've got would be awesome!
EDIT: For clarity's sake, this element is the rows per page drop down:
<span id="rppSpan">
<select id="yui-pg0-1-rpp24" class="yui-pg-rpp-options" title="Rows per page">
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
</span>
Subscribe to YAHOO.widget.Paginator's rowsPerPageChange instead:
http://developer.yahoo.com/yui/docs/YAHOO.widget.Paginator.html#event_rowsPerPageChange
Then you don't have to find the actual element.