【问题标题】:Iterating over a list stored in webElement遍历存储在 webElement 中的列表
【发布时间】:2018-11-20 15:24:10
【问题描述】:

我正在使用 python 学习 appium(移动自动化)。

我有一个场景,我有一个 listView,我必须遍历每个项目,然后单击返回,直到所有项目都被单击。

我正在使用以下代码:

def test_selectingEveryOption(self):
        availableOptions = self.driver.find_elements_by_xpath('//android.widget.ListView')
        for options in availableOptions:
            availableOptions[options].click()
            self.driver.back()

availableOptions 是具有列表的 webElement。当我在上面运行一段代码时,我得到TypeError: list indices must be integers or slices, not WebElement

由于 availableOptions 是 webElement,我怎样才能将其列表项作为整数获取,然后对其进行迭代?

【问题讨论】:

  • 有可用的选项列表吗?
  • 不,availableOptions 是 WebElement 对象的列表,find_elements_by_xpath 总是返回一个列表。

标签: python python-3.x automation appium python-appium


【解决方案1】:

browser.find_elements_by_class_name("myClass") 返回一个 WebElement 列表。所以.. 在你的 for 循环中,每次迭代都会产生一个 WebElement,而不是整数索引......你根本不需要使用索引。

for element in self.driver.find_elements_by_xpath('//android.widget.ListView'):
    element_contents = element.get_attribute('innerHTML')

【讨论】:

  • 但是我得到了列表的 noSuchElementException .. NoSuchElementException: 消息:这个元素没有 'innerHTML' 属性
  • 我对 python-appium 不是很精通,但是您如何找出在循环中打印与“元素”相关的所有属性,看看哪一个适合您的需求m
猜你喜欢
  • 2018-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多