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

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

Related

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] + " ");
}

Incrementing value with Selenium IDE

How do I increment value of img path when said path looks like this?
//ab[x]/img
X value increasing by 1 and has a limit of 50.
Trying to write a test case on how to click on several images on website.
Edit: Just wanted to add that I'm just starting with Selenium IDE and using standart commands.
Solution 1: Format your xpath path selector
for(int i=1; i<=numberOfImages; i++) {
String path = String.format("//ab[%d]/img", i);
WebElement image = driver.findElement(By.xpath(path));
if(image != null) {
image.click();
}
}
Solution 2: Select all elements that "//ab/img" returns and iterate over them.
String path = "//ab/img";
List<WebElement> imgElements = driver.findElements(By.xpath(path)); //notice the plural
for(WebElement image : imgElements) {
image.click();
}

How to take all the dropdown options?

In selenium 2.0, i am trying to get the list of drop down values and to print it. How to do this? I am trying below:
for (int i = 1;i<=13;i++)
{
WebElement values=driver.findElement(By.xpath("//li[#rel='i']/a/span[#class='pull-left']"));
System.out.println(values);
}
#rel= '1', '2' should go like this.. so that i can print all the values.
But this is not working.. How to use the 'i' in this element.
Thanks
Instead of hard coding no of options value you can get that dynamically.
List<WebElement> options = driver.findElements(By.xpath("//ul/li/a/span[#class='pull-left']"));
//iterate above list to get all option values
for(WebElement eachOption : options) {
System.out.println(eachOption.getText());
}
Some how i managed to get this..
for (int i = 1;i<=13;i++) {
//System.out.println("//li[#rel=" + i +"]/a/span[#class='pull-left']");
String values=driver.findElement(By.xpath("//li[#rel=" + i +"]/a/span[#class='pull-left']")).getText();
System.out.println(values); }

How to get style sheet property value by webdriver?

I want to know whether content menu of drop down is displayed or none. I want to click on that menu if it's display none. and keep as it is if blocked. the property value is like this:
div id="section_content_23" class="thread-content" style="display: none;" i have xpath of title is like ex 'section_title_23'. i just want to know the style is "display: none;" or style = "display: blocked";
I have write code for this like:
else if (str.contains("section_title_"))
{
//String xpath;
String[] retval = str.split("_", 3);
String no = retval[2];
//
displaylinkhandler = QuickCap.driver.findElement(By.id("section_content_" + no)).getAttribute("style");
//displaylinkhandler = QuickCap.driver.findElement(By.id("section_content_" + no)).getAttribute("section_content_"+no);
//displaylinkhandler = QuickCap.driver.findElement(By.xpath(xpath)).getText();
//boolean show = displaylinkhandler.contains("display: none;");
if(displaylinkhandler == null){
QuickCap.driver.findElement(By.xpath(str)).click();
}
indexBoolean = TitleCheck.validate();
}
here str is xpath & displaylinkhandler is like string.
Thanks in advance.
did you tried getCssValue from webelement.
for instance: ele.getCssValue("propname")
By the way to check element is displayed or not you should user isDisplayed method of webelement
In webdriver we use
WebElement element = driver.findElement(By.name("elementName"));
and then
String attributeValue = element.getAttribute("attributeName");
So in your case it will
String styleValue = element.getAttribute("style");

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.