I want to know how to nest dropdowns using selenium webdriver using java,i.e., I have 2 dropdowns and can these dropdowns be nested one after the other?
After looping 2 times for a dropdown it is showing stale element reference error
I have written the following code:
Select drpdwns6 = new Select(driver.findElement(By.xpath("//*[#id=\"MainContent_ddlBillable\"]")));
List <WebElement> sels6 = drpdwns6.getOptions();
sels6.size();
for(int s6=0;s6<sels6.size();s6++) {
drpdwns6.selectByIndex(s6);
System.out.println("selected value"+s6);
Select drpdwns7 = new Select(driver.findElement(By.xpath("//*[#id=\"MainContent_ddlofflinestatus\"]")));
List <WebElement> sels7 = drpdwns7.getOptions();
sels7.size();
for(int s7=0;s7<sels7.size();s7++) {
drpdwns7.selectByIndex(s7);
System.out.println("selected value"+s7);
}
}
My guess is selecting the option from the dropdown refreshes the DOM, so the exception is thrown. You need to relocate the dropdown in each itreation
Select drpdwns6 = new Select(driver.findElement(By.id("MainContent_ddlBillable")));
int drpdwns6Size = drpdwns6.getOptions().size();
for(int s6 = 0 ; s6 < drpdwns6Size ; s6++) {
drpdwns6 = new Select(driver.findElement(By.id("MainContent_ddlBillable")));
drpdwns6.selectByIndex(s6);
System.out.println("selected value"+s6);
Select drpdwns7 = new Select(driver.findElement(By.id("MainContent_ddlofflinestatus")));
int drpdwns7Size = drpdwns7.getOptions().size();
for(int s7 = 0 ; drpdwns7Size ; s7++) {
drpdwns7 = new Select(driver.findElement(By.id("MainContent_ddlofflinestatus")));
drpdwns7.selectByIndex(s7);
System.out.println("selected value"+s7);
}
}
As a side note, if you have an id use By.id it instead of By.xpath
You get the Stale element exception whenever the element present in the DOM is deleted or removed or is not available.
The above answer (ie) relocating the element once the DOM is refreshed or you could use Webdriver wait, If an element is not attached to DOM then you could try using ‘try-catch block’ within ‘for loop’ like below
driver.manage().timeouts().implicitlywait(30,TimeUnit.SECONDS);
try{
Select drpdwns6 = new
Select(driver.findElementByXpath("//[#id=\"MainContent_ddlBillable\"]")));
List <WebElement> sels6AllOptions = drpdwns6.getOptions();
int count1=sels6AllOptions.size();
for(int s6=0;s6<count1;s6++)
{
drpdwns6.selectByIndex(s6);
}
}
catch(StaleElementException e1){
System.out.println("selected value"+s6);
}
try{
Select drpdwns7 = new Select(driver.findElement(By.xpath("//*[#id=\"MainContent_ddlofflinestatus\"]")));
List <WebElement> sels7AllOptions = drpdwns7.getOptions();
int count2=sels7AllOptions.size();
for(int s7=0;s7<count2;s7++) {
drpdwns7.selectByIndex(s7);
catch(StaleElementException e2){
System.out.println("selected value"+s7);
}
}
i am using maps.mapmyindia.com, when i am searching for a state or city it shows a list of search list now i need to print the index of the exact match of my search, i want to print that index on the consol, thanks in advance. i have tried this code
WebElement list = driver.findElement(By.xpath("//div[#class='directions-route-text']"));
String str = "Nehru Place";
WebElement li = list.findElement(By.xpath("*[. = str]"));
List<WebElement> myElements = driver.findElements(By.tagName("li"));
System.out.println(myElements.indexOf(li));
List<WebElement> myElements = list.findElements(By.tagName("li"));
ArrayList<String> as = new ArrayList<String>();
int listsize= myElements.size();
for(int i=0;i<listsize;i++)
{
as.add(myElements.get(i).getText());
}
System.out.println(as.indexOf(str));
Try above code:
I am using Appium and I want to print names of the elements in the list.
I am using following code
List<WebElement> list = getDriver().findElementsByXPath(getLocator(Locators.MY_ITEM));
List<String> strings = new ArrayList<>();
for (WebElement object : list) {
String text = object.getText();
logger.info(text);
if (!text.isEmpty())
strings.add(text);
}
But I am getting text always as empty.
What is the suggested approach over here.
Note each element is of type UIACollectionCell in case of iOS and on Android //android.widget.TextView[#text='%s']
From what I understand, you should be getting the text from the text attribute, replace:
String text = object.getText();
with:
String text = object.getAttribute("text");
<select id="vehicleTypeName" class="custom-select" name="vehicleTypeName">
<option value="">-Select Style-</option>
<option value="Hatchback">Hatchback Cars</option>
<option value="Sedans">Sedan Cars</option>
<option value="MUV">MPV Cars</option>
<option value="Sport Utilities">SUV Cars</option>
<option value="Luxury Vehicles">Luxury Cars</option>
<option value="Hybrids">Hybrid Cars</option>
<option value="Minivans">Minivans</option>
<option value="Convertibles">Convertible Cars</option>
<option value="Coupe">Coupe Cars</option>
</select>
i have used this code to validate the options
String label=driver.findElement(By.id("vehicleTypeName")).getText();
logger.info("Brand names are \t" + label );
But it is printing all the brands including "-Select Style-" this item. but I don't want to print this "-Select Style-" value.but I want to validate only brand names. please help me with this.
I just wrote a quick function for you. hope it helps!
/**
* Get all <code><option/></code> innerHTML attributes
*
*/
List<String> getAllOptions(By by) {
List<String> options = new ArrayList<String>();
for (WebElement option : new Select(driver.findElement(by)).getOptions()) {
String txt = option.getText();
if (option.getAttribute("value") != "") options.add(option.getText());
}
return options;
}
Now executing:
getAllOptions(By.id("vehicleTypeName"));
will return:
["Hatchback Cars", "Sedan Cars"...] // of course in List<> representation..
Can Use below use it worked for me
Select dropDown = new Select(Driver.findElement()));
List<WebElement> e = dropDown.getOptions();
int itemCount = e.size();
for(int l = 0; l < itemCount; l++)
{
System.out.println(e.get(l).getText());
}
Easy solution;
String label = driver.findElement(By.id("vehicleTypeName")).getText().replace("-Select Style-", "");
BTW, it's not a good practice.
List<WebElement> options = driver.findElements(
By.xpath("//*[#id="vehicleTypeName"]/option"));
Now you can easily construct a new List from options excluding the first element:
List<String> text = new ArrayList<>();
for(int i=1; i<options.size(); i++) {
text.add(options.get(i).getText());
}
Hi the below method will solve your problem. It will be easy for you to use
public List<String> getAllValues() throws InterruptedException
{
Select sel = new Select(DomainName);
List<WebElement> we = sel.getOptions();
List<String> ls = new ArrayList<String>();
for(WebElement a : we)
{
if(!a.getText().equals("Select")){
ls.add(a.getText());
}
}
return ls;
}
WebElement element = driver.findElement(By.xpath("Your xpath here"));
Select select = new Select(element);
List<WebElement> list = select.getOptions();
for(int i=0; i<list.size(); i++)
{
System.out.println(list.get(i).getText());
}
All the above lines are continuous piece of code.
Working :
a) Point 1 and 2 are straight forward to locate a Drop down box
b) In point 3, we create a List variable 'list' and stuff all the values in the drop down.
c) In the for loop, we pick each element according to their index using get(i) and use getText() to acquire the text it holds.
Hope it helps.
You could try this:
String label=driver.findElement(By.id("vehicleTypeName"));
Select labelSelector = new Select(label);
labelSelector.deselectByValue("");
List<WebElement> allOptions = labelSelector.getOptions();
You could then use the List and it should contain all options that have something in the value tag.
Below are the possible ways to do it:
1- By removing the first Option from the list
//Locating the Select Dropdown for Vehicle Type
Select sel = new Select(driver.findElement(By.id("vehicleTypeName")));
//Getting all the options present in the dropdown.
List<WebElement> list = sel.getOptions();
//Removing the first dropdown option from the list, i.e., '-Select Style-'
list.remove(sel.getFirstSelectedOption());
//Printing the List
System.out.println("The list without the first element, i.e., 'Select Type' is as under: ");
for(WebElement ele: list)
System.out.println(ele.getText());
2- By removing the option that has text "-Select Style-"
//Locating the Select Dropdown for Vehicle Type
Select sel = new Select(driver.findElement(By.id("vehicleTypeName")));
//Getting all the options present in the dropdown.
List<WebElement> list = sel.getOptions();
//Removing the dropdown option '-Select Style-' from the list
Iterator<WebElement> iter = list.iterator();
while(iter.hasNext()){
if(iter.next().getText().equals("-Select Style-")){
iter.remove();
break;
}
}
//Printing the List
System.out.println("The list without the first element, i.e., 'Select Type' is as under: ");
for(WebElement ele: list)
System.out.println(ele.getText());
Try this one
WebElement lelement = driver.findElement(By.id("vehicleTypeName"));
Select oSelect = new Select(lelement);
java.util.List <WebElement> elementCount = oSelect.getOptions();
int iSize = elementCount.size();
String [] arrdropdown= new String [iSize];
for (int j = 0; j < iSize; j++)
{
arrdropdown[j]=elementCount.get(j).getText();
}
logger.info("Brand names are \t" + Arrays.toString(arrdropdown));
//**Selecting all Option Using tagName as Option in List and run foreach loop**
for(WebElement Element:driver.findElements(By.tagName("option")))
{System.out.println(Element.getText());}
I am trying to get the value selected from the dropdown using the below snippet of code:
Select dropdown = new Select(driver.findElement(By.xpath(prop.getProperty("items"))));
WebElement tmp = dropdown.getFirstSelectedOption();
tmp.getText();
String s = tmp.getText();
System.out.println(s);
When I run the application it gives me following error:
org.openqa.selenium.StaleElementReferenceException: Element not found
in the cache - perhaps the page has changed since it was looked up
Thank You
boolean flag = false;
Select dropdown = new Select(driver.findElement(By.xpath(prop.getProperty("items"))));
List<WebElement> list = dropdown.getOptions();
for (WebElement temp : list) {
if (temp.getText().equals(prop.getProperty("your value").toString())) {
flag = true;
}
}