I want to scroll to the element and click on it

There is a element which is not visible, but after scrolling it is visible.

Sometimes it appears even before scrolling, sometimes after scrolling.

how to apply the logic,the element should able to click, such that it should even before and after the element is visible.

You could do something like this:

// first try if the element is clickable
try {
driver.findElement(By.id(“whatever”)).click();
} catch (ElementNotFoundException e) {
//if element is not present
driver.swipe(“swipe_to_the_new_coordinates_desired”);

// and then try again
driver.findElement(By.id(“whatever”)).click();
}

or something on these similar lines. There are some deprecated API’s like scrollTo() and scrollToExact(), which you might want to try if it helps.

Cheers!!

Put a while or for loop for a while, and scroll down until the element to be appeared.

e.g.

while(!driver.findElement(xyz).isDisplayed())
{
if(driver.findElement(xyz).isDisplayed())
{
driver.findElement(xyz).click();
break;
}
scroll Action();
}

Regards,
Bhaskar.

thnks you so much, i will try both the code and check which one is working

in case your element located inside scrollable view, you can use following locator:

@AndroidFindBy(uiAutomator = “new UiScrollable(.scrollable(true))” +
“.scrollIntoView(.description("your_element_test"))”)

check out android documentation about UiScrollable and other uiautomator selectors UiScrollable  |  Android Developers

could u please elabarate on how to mention the id or xpath or the className

could you please spend some time studying the link i provide above?

I went through that link

the following code i wrote but it is not working

@AndroidFindBy(uiAutomator = “new UiScrollable(.scrollable(true))” +
“.scrollIntoView(.description(“xpath=//android.support.v7.widget.RecyclerView/android.widget.RelativeLayout[5]/android.widget.RelativeLayout[@index=‘2’]”)))”)
public WebElement StatusOfAssignment1;

public void ClickOnAssignmentButton() throws InterruptedException{

	StatusOfAssignment1.click();
}

org.openqa.selenium.UnsupportedCommandException: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 3.90 seconds