Have to handel allow button for location

HI,

i am testign on cloud on 10 devices at a time and on some devices the allow button is there and some dont ask when allow button is displayed it works fine and go in if condition but when its not displayed it fails here giving below exception can anyone help me with this hot to handel dynamically.
Thanks Again
if(driver.findElementById(“com.android.packageinstaller:id/permission_allow_button”).isDisplayed());

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 983 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: ‘2.53.0’, revision: ‘35ae25b1534ae328c771e0856c93e187490ca824’, time: ‘2016-03-15 10:43:46’
System info: host: ‘lubnas-MacBook-Air.local’, ip: ‘192.168.188.236’, os.name: ‘Mac OS X’, os.arch: ‘x86_64’, os.version: ‘10.12.3’, java.version: ‘1.8.0_111’
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{app=/Users/lubnashaikh/Desktop/Appium_testing/src/weatherzone-5.0.0-500-dev-beta.apk, appPackage=au.com.weatherzone.android.weatherzonefreeapp.beta, networkConnectionEnabled=true, warnings={}, appWaitActivity=au.com.weatherzone.android.weatherzonefreeapp.IntroActivity, appWaitPackage=au.com.weatherzone.android.weatherzonefreeapp.beta, databaseEnabled=false, deviceName=emulator-5554, avd=Nexus_5_API_23, platform=LINUX, deviceUDID=emulator-5554, appActivity=au.com.weatherzone.android.weatherzonefreeapp.LauncherActivity, desired={app=/Users/lubnashaikh/Desktop/Appium_testing/src/weatherzone-5.0.0-500-dev-beta.apk, appPackage=au.com.weatherzone.android.weatherzonefreeapp.beta, newCommandTimeout=3000, platformVersion=6.0, locationContextEnabled=true, appWaitActivity=au.com.weatherzone.android.weatherzonefreeapp.IntroActivity, automationName=Appium, platformName=Android, deviceName=Android Emulator, avd=Nexus_5_API_23}, newCommandTimeout=3000, platformVersion=6.0, webStorageEnabled=false, locationContextEnabled=true, automationName=Appium, takesScreenshot=true, javascriptEnabled=true, platformName=Android}]
Session ID: 71932a3a-12e4-4953-8b64-d2e78c8ee7a5
*** Element info: {Using=id, value=com.android.packageinstaller:id/permission_allow_button}

You don’t say what programming language so I’m going to do this in Ruby, which is what I use:

is_displayed_button = nil
begin
  is_displayed_button = driver.findElementById("com.android.packageinstaller:id/permission_allow_button").isDisplayed());
rescue NoSuchElementException
  # Don't need to do anything here as the button will be nil
end

if is_displayed_button
  <do whatever it is you need to do>
else
  <do the other thing>
end

So don’t think of this as an Appium issue, but a programming one. Surround the find in a rescue, recover, make a decision, finish the test.