[Android] [Python] How Do You Check a Checkbox

Hello I’m trying to find a way to check a checkbox after verifying that the checkbox is not checked.

I got appium to access the device’s Display settings and I’m trying to make it so that the screen won’t auto-rotate.

Also, I got it to find the element with the text “Auto-rotate screen”, but I wasn’t sure how to actually select the “CheckBox” element that is associated with “Auto-rotate screen” option…

def setUp(self):
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '4.4.4'
    desired_caps['deviceName'] = 'Android Device'
    desired_caps['appPackage'] = 'com.android.settings'
    desired_caps['appActivity'] = 'com.android.settings.Settings'

    self.driver = webdriver.Remote(
        'http://localhost:4723/wd/hub', desired_caps)

def tearDown(self):

def test_config_display(self):
        'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("Display").instance(0));').click()

    auto_rotate = self.driver.find_element_by_android_uiautomator('new UiSelector().text("Auto-rotate screen")')

I’d appreciate any help, thanks.

This is two step process -
First you need to get the handle to checkbox. This can be done in multiple ways depending on how your screen looks like.

  • You can use ID, if checkbox has an unique id.
  • You can use Index if there are few checkboxes and their index does not change in the screen [i.e., find all check boxes and index to the one you want to act upon]
  • You can use xPath to find the checkbox

Once you got hold of Checkbox, use the element.getAttribute(“checked”) to check if its selected or not.

1 Like

Hi RamS! Could you explain a little more your option two please?