如何使用Appium和Python在同一类中单击多个按钮

如何使用Appium和Python在同一类中单击多个按钮,python,appium,Python,Appium,我正在尝试创建一个机器人,它将自动释放Depop上所有当前跟踪的用户 我可以得到它,只要不跟随后面用户列表中的第一个用户 问题是,所有“取消跟随”按钮都使用同一个类和文本“Following” 我已经创建了一个while循环,我希望它能点击所有当前显示的包含文本“Following”的按钮。然而;它只会“单击”第一个按钮 unfollow = 'yes' while unfollow == 'yes': if driver.find_element_by_clas

我正在尝试创建一个机器人,它将自动释放Depop上所有当前跟踪的用户

我可以得到它,只要不跟随后面用户列表中的第一个用户

问题是,所有“取消跟随”按钮都使用同一个类和文本“Following”

我已经创建了一个while循环,我希望它能点击所有当前显示的包含文本“Following”的按钮。然而;它只会“单击”第一个按钮

    unfollow = 'yes'
    while unfollow == 'yes':
        if driver.find_element_by_class_name('android.widget.Button'):
        followBtn = driver.find_element_by_class_name('android.widget.Button')
        btnText = followBtn.text

        if btnText == 'Following':
            followBtn.click()
        else:
            unfollow = 'no'

您应该使用
driver.find_elements()
并将所有元素存储在字典中并进行迭代。 最后,再次检查是否存在任何按钮,并且您不能使用driver.find()作为条件,您应该使用try/except块来达到此目的

这就是答案:

unfollow = 'yes'
while unfollow == 'yes':
  try:
    driver.find_element_by_class_name('android.widget.Button')
  except:
    return
  elements = driver.find_elements_by_class_name('android.widget.Button')
  for el in elements:
  btnText = el.text
  if btnText == 'Following':
      el.click()
  else:
    unfollow = 'no'
    return