【问题标题】:Python desired capabilities errorPython 所需功能错误
【发布时间】:2017-12-31 18:13:51
【问题描述】:

设置所需功能 (desired_caps) 后,我的脚本出现错误。错误是

if desired_capabilities is None:
^ IndentationError: 意外缩进

代码如下:

import unittest
from selenium import webdriver
from appium import webdriver
from appium import webdriver
from selenium.webdriver.common import desired_caps
from webdriver import WebDriver as Remote
class CNNEndToEnd(unittest.TestCase):
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '6.0.1'
    desired_caps['deviceName'] = 'Samsung'
    #desired_caps['app'] = PATH('../../../apps/selendroid-test-app.apk')
    desired_caps['appPackage'] = 'com.android.vending'
    desired_caps['appActivity'] = '.AssetBrowserActivity'
    print desired_caps
    self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)


    def testACNNInstallAndLaunch(self): 
        print "**************************"
        print "********APP INSTALL*******"
        print "**************************"
        print "here---------"
        #appid = "com.cnn.mobile.android.phone"
        if (self.driver.is_app_installed("com.cnn.mobile.android.phone")):
            print "-----App ALready Installed"
if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(CNNEndToEnd)
    unittest.TextTestRunner(verbosity=2).run(suite)

完整的错误信息:

Traceback (most recent call last):
  File "/Users/manusimply/.p2/pool/plugins/org.python.pydev_5.8.0.201706061859/pysrc/_pydev_runfiles/pydev_runfiles.py", line 468, in __get_module_from_str
    mod = __import__(modname)
  File "/Users/manusimply/Desktop/Eclipse/CNN_PoC/CNN_New.py", line 2, in <module>
    from appium import webdriver
  File "/Users/manusimply/Desktop/Eclipse/Appium-Python-Client-0.2/appium/webdriver/__init__.py", line 19, in <module>
    from webdriver import WebDriver as Remote
  File "/Users/manusimply/Desktop/Eclipse/Appium-Python-Client-0.2/appium/webdriver/webdriver.py", line 15, in <module>
    from selenium import webdriver
  File "/Users/manusimply/Desktop/Eclipse/selenium-3.4.3/selenium/webdriver/__init__.py", line 18, in <module>
    from .firefox.webdriver import WebDriver as Firefox  # noqa
  File "/Users/manusimply/Desktop/Eclipse/selenium-3.4.3/selenium/webdriver/firefox/webdriver.py", line 34, in <module>
    from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
  File "/Users/manusimply/Desktop/Eclipse/selenium-3.4.3/selenium/webdriver/remote/webdriver.py", line 79
    if desired_capabilities is None:
    ^
IndentationError: unexpected indent
ERROR: Module: CNN_New could not be imported (file: /Users/manusimply/Desktop/Eclipse/CNN_PoC/CNN_New.py).

【问题讨论】:

  • 你的问题是什么?
  • 对不起,因为我是这个社区的新手,所以我对询问查询没有正确的想法。从上面的代码中我无法运行我的脚本。
  • stackoverflow.com/help/how-to-ask 这应该让你继续提问。
  • 请说明您希望看到的内容、您看到的内容以及您为尝试解决问题所做的工作。
  • 谢谢,我在上面运行代码时遇到了以下问题

标签: python eclipse selenium


【解决方案1】:

您应该使用 DesiredCapabilities 对象,而不仅仅是您想要实现的功能列表。它会让你的生活更轻松一些。如下所示初始化变量,然后将您的能力添加到该变量中。

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_caps = DesiredCapabilities.CHROME

有关在 python 中初始化 webdriver 的更多详细信息,请参阅此链接: http://selenium-python.readthedocs.io/getting-started.html

还要确保您为每个功能使用正确的命名约定。这里有一个功能列表及其名称:https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities

希望对您有所帮助:)

【讨论】:

  • 谢谢,设置所需的功能,但问题仍然存在。
  • 查看您的堆栈跟踪,您可能没有正确安装 selenium。您是否按照selenium-python.readthedocs.io/installation.html 上的说明进行操作??该网站应该准确地告诉您如何开始
  • 你能帮我如何创建驱动程序对象,我正在尝试在机器人下面显示code AttributeError: 'CNNEndToEnd' 对象没有属性'驱动程序' code self.driver = webdriver.android ('localhost:4723/wd/hub', desired_caps)
【解决方案2】:
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0.1'
desired_caps['deviceName'] = 'Samsung'
#desired_caps['app'] = PATH('../../../apps/selendroid-test-app.apk')
desired_caps['appPackage'] = 'com.android.vending'
desired_caps['appActivity'] = '.AssetBrowserActivity'
**print(desired_caps)**

也适用于desired_caps。我建议包含 udid 并使用完整的 appActivity 路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-29
    • 2018-08-22
    • 1970-01-01
    • 2023-03-28
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多