Error on trying to click a radiobutton using "Select" class | Selenium Forum
M
Posted on 17/12/2015
Following is the code:

package p111;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Radiobuttons {

public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver w=new FirefoxDriver();
w.get("http://www.echoecho.com/htmlforms10.htm");

List<WebElement> L=new ArrayList <WebElement>();
L=w.findElements(By.name("group1"));
System.out.println(L.size());
Select s;
for(int i=0;i<L.size();i++){

System.out.println("---"+L.get(i).getAttribute("value"));
//L.get(i).click();
try{
s=new Select(L.get(i));

}
catch(Exception e)
{
System.out.println("---caught");
}finally{
s.selectByIndex(i); //<---------====================Local variable s may not be resolved
}

System.out.println(L.get(i).getAttribute("checked"));
}


}

}

M
Replied on 17/12/2015

what is the error?


M
Replied on 18/12/2015

s.selectByIndex(i); //<---------====================Local variable s may not be resolved


M
Replied on 18/12/2015

i cannot understand your code.


M
Replied on 19/12/2015

Have put some comments in code :
-------------------------------------
package p111;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Radiobuttons {

public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver w=new FirefoxDriver();
w.get("http://www.echoecho.com/htmlforms10.htm");

//----L is reference of List class---------------------
List<WebElement> L=new ArrayList <WebElement>();
L=w.findElements(By.name("group1"));
System.out.println(L.size());
//-----------------------
Select s;//Selcet class reference creation
//------------------------

for(int i=0;i<L.size();i++){

System.out.println("---"+L.get(i).getAttribute("value"));

try{
s=new Select(L.get(i));

}
catch(Exception e)
{

}
//=======================================================================================
finally{
s.selectByIndex(i); //<----This line i get error:-Local variable s may not be resolved
}
//=======================================================================================
System.out.println(L.get(i).getAttribute("checked"));
}


}

}


M
Replied on 21/12/2015

you have to initiate[color=#FF0000:3n8moqob] s=new Select(L.get(i));[/color:3n8moqob] outside try catch block.


M
Replied on 21/12/2015

I get org.openqa.selenium.support.ui.UnexpectedTagNameException for the line highlighted below:

package p111;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Radiobuttons {

public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver w=new FirefoxDriver();
w.get("http://www.echoecho.com/htmlforms10.htm");

//----L is reference of List class---------------------
List<WebElement> L=new ArrayList <WebElement>();
L=w.findElements(By.name("group1"));
System.out.println(L.size());
int i=0;
//======================================================================================
Select s=new Select(L.get(i));////<---------org.openqa.selenium.support.ui.UnexpectedTagNameException
//===================================================================================

for(i=0;i<L.size();i++){

System.out.println("---"+L.get(i).getAttribute("value"));
//s=new Select(L.get(i));
try{


}
catch(Exception e)
{

}finally{
s.selectByIndex(i);
}

System.out.println(L.get(i).getAttribute("checked"));
}


}

}


M
Replied on 21/12/2015

[quote:3lxvjj3t]
org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input"
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'[/quote:3lxvjj3t]

your program will not work because Select is for drop down menus and you're using it on radio buttons.