Spinner(Drop down) selection in Android Mobile APP


#1

login screen.

  1. In Login screen (Country, User name, Password) available.
  2. Country is Spinner(drop down list) & data is in alphabetical order.
  3. I need to select ex “United Kindom”.
    How to select that country.

Spinner opening time showing 6 countries(America, Australia, etc. I can select those countries.

driver.findEliment(By.id(“spinner”)).click();
driver.findElement(By.name(“America”);

So America is selecting No problem is this case.

Now i Need to select “United Kingdom”

driver.findEliment(By.id(“spinner”)).click();
driver.findElement(By.name(“United Kingdom”);

not working
I need to scroll right?
How to scroll, can you tell me please…


#2

hi guys.

please help me


#3

Hi,

You can use javascript to scroll window.
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(“window.scrollBy(0,3000)”,"");

0,3000 - x,y co-ordinates are based on screen size

Thanks,
Sujata


#4

after click on spinner ,you can use
driver.scrollToExact(“United Kingdom”)


#5

thank’Q
sujata_kalluri & Aravind for your respnce…

Thank’Q Arvind its working…


#6

One more think
I need to select United Kingdom in the spinner.
I used driver.scrollToExact(“United Kingdom”);
I need to Pic the exact word ‘United Kingdom’
In my list 300 Country’s are available
it is scrolling slowly like user swapping.
How to pic exact word???


#7

Use getText command immediate after driver.scrollToExact(“United Kingdom”);
like
driver.scrollToExact(“United Kingdom”);
driver.findElement(By.(locator)).getText();

i am using same for click(After scroll ), so i think for getText also it should word.


#8

HI Arvind,

thank’Q

driver.scrollToExact(“United Kingdom”);
driver.findElement(By.(locator)).getText();

can you tell me in (locator) place what i will give.


#9

driver.scrollToExact(“United Kingdom”);
driver.findElementByName(“United Kingdom”).getText();

Locator could be Name,Id,Xpath,Content


#10

Hi Arvind,

I am not able to select the state by using below code
driver.scrollToExact(“Uttar Pradesh”);
driver.findElementByName(“Uttar Pradesh”).getText();

Code is trying to pick the Uttar Pradesh state but numeric keyboard was already opened for mobile number field.

Can you please help me, how to hide the keyboard? so that i could call below script
river.scrollToExact(“Uttar Pradesh”);
driver.findElementByName(“Uttar Pradesh”).getText();


#11

You can provide the index of the element and then use sendKeys method for the text to select.

driver.findElements(By.xpath(xpathSelector)).get(index).sendKeys(text);


#12

It scrolls but doesnt click. what did you do? I am using the exact code what you mentioned here


#13

scroll to text is not working, do you guys another way to simular scroll to text


#14

Hi,

driver.findElement(By.name(“America”);

is the above is working for Drop down, as to select any drop down we need Select class ? Can you lease update me ?


#15

By.name has been deprecated.
Try using findElementByAndroidUIAutomator
Eg:
driver.findElementByAndroidUIAutomator(“new UiSelector().text(“America”)”).click();


#16

try this

public void swipeUpUntilTextExists(String expectedText, int howManyTimesToSwipe) {
	int i = 0;
	do {
		swipeUP();
		i++;
		if (i == howManyTimesToSwipe)
			break;
	} while (!driver.getPageSource().contains(expectedText));
}

swipe method:
public void swipeUP(){
Dimension size = driver.manage().window().getSize();
log.info(size);
// calculate coordinates for vertical swipe
int startVerticalY = (int) (size.height * 0.8);
int endVerticalY = (int) (size.height * 0.2);
int startVerticalX = (int) (size.width / 2);
new TouchAction(driver).press(point(startVerticalX, startVerticalY)).waitAction(waitOptions(ofSeconds(3))).moveTo(point(startVerticalX, endVerticalY)).release().perform();
}

or this with uiAutomator:
@AndroidFindBy(uiAutomator = “new UiScrollable(new UiSelector()).scrollIntoView(text(“Tokyo”))”)
public MobileElement tokyo;