Reputation: 39
I logged into an an app for signup using Selenium, where I had to select a country from a country list .How can I select a specific country e.g. "India" from the "Country" dropdown using an appium command?
I tried using:
driver.scrollToExact("India").click()
but it did not work.
Upvotes: 1
Views: 11182
Reputation: 203
Firs write below method
public void **scrollToExactElement**(String str) {
((AndroidDriver<MobileElement>) driver).findElementByAndroidUIAutomator(
"new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\""
+ str + "\").instance(0))").click();
}
//this will scroll your list driver.switchTo();
now write scrollToExactElement("india");
Upvotes: 0
Reputation: 188
You can click on spinner then find the country "india" from drop down. Try this
driver.findElement(By.id("xpath_of_spinner")).click();
driver.findElement(By.name("india")).click();
Upvotes: 1
Reputation: 171
A better approach is to use XPath to find the location of the dropdown and then again find the suitable XPath to locate the element that you want to select.If you have any clarifications regarding how to find the XPath, just let me know.
Upvotes: 0
Reputation: 1370
You can scroll down until element is found:
public void scrollDownAndClickIfElementPresent(String name) {
for (int i = 0; i < 10; i++) {
if (isElementPresent(name)) {
tapOnElement(name);
break;
}
else{
scrollDown();
}
}
public void scrollDown() {
Dimension size = driver.manage().window().getSize();
int x = size.width / 2;
int starty = (int) (size.height * 0.60);
int endy = (int) (size.height * 0.10);
driver.swipe(x, starty, x, endy, 2000);
}
public boolean isElementPresent() {
try {
driver.findElementByName("india");
return true;
} catch (Exception e) {
return false;
}
}
}
Upvotes: 0