【问题标题】:'dict' object has no attribute 'driver''dict' 对象没有属性 'driver'
【发布时间】:2021-03-08 14:27:03
【问题描述】:

我是 web 自动化和 python 的新手,所以请理解我的问题对你们来说可能是微不足道的。我正在尝试使用 Appium 来测试应用程序并按下屏幕上的按钮。使用我的最低知识,我在下面创建了一个测试用例,目的是为了做到这一点,但我得到一个错误 'dict' object has no attribute 'driver'

from appium import webdriver
import time

def setUp():
    self = dict(
        platformName = "Android",
        platformVersion = "10",
        deviceName= "name",
        app= "C:\\AppiumProjects\\app-debug.apk",
        automationName= "UiAutomator2",
        appActivity = ".activities.SplashScreenActivity",
        appPackage = 'genericsurveyapp2',
        noReset = True,
        
        )
        
    self.driver = webdriver.Remote('http://localhost:4723/wd/hub',self)
    time.sleep(2)
    time.sleep(2)
    self.el = self.driver.find_element_by_id("idacceptButton")
    time.sleep(2)
    self.el.click

setUp()

【问题讨论】:

  • 不要在变量声明中使用点 (.)。试试这个:self_driver = .........

标签: python mobile automation appium


【解决方案1】:

我认为您对自己的配置选项字典命名感到困惑。 在 self 类中工作时,允许您使用“点符号”引用类的变量和方法。但是,使用字典“点表示法”可以让您访问字典类的变量和方法,而不是字典的内容。

你基本上有 3 个选项来解决这个问题:

  1. 使用字典符号将元素添加到字典中。我不建议这样做,因为您在同一个字典、配置选项、驱动程序对象和页面元素中存储了 3 种不同类型的东西。
self['driver'] = webdriver.Remote('http://localhost:4723/wd/hub',self)
self['el'] = self['driver'].find_element_by_id("idacceptButton")
self['el'].click()
  1. driverel 分配为函数本地上下文中的变量。如果您打算在设置后丢弃配置、驱动程序和页面元素,这是我要采用的解决方案。
driver = webdriver.Remote('http://localhost:4723/wd/hub',self)
el = driver.find_element_by_id("idacceptButton")
el.click()
  1. 重构代码以使用遵循页面对象模式的类。这可能是您想要做的,因为它符合 appium 测试最佳实践。

【讨论】:

    猜你喜欢
    • 2017-03-30
    • 2012-01-12
    • 2017-05-12
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 2021-01-30
    相关资源
    最近更新 更多