Click元素在Appium中不起作用,发生'java.lang.NullPointerException'

时间:2018-01-10 03:40:43

标签: selenium appium mobile-application appium-android

Eclipse控制台错误:

Exception in thread "main" java.lang.NullPointerException
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:279)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:83)
    at amazon.StartApplication.main(StartApplication.java:58)

Appium日志:

    {"strategy":"id","selector":"in.amazon.mShop.android.shopping:id/sign_in_button","context":"","multiple":false}}
    [debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"in.amazon.mShop.android.shopping:id/sign_in_button","context":"","multiple":false}}
    [debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
    [debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
    [debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'in.amazon.mShop.android.shopping:id/sign_in_button' using 'ID' with the contextId: '' multiple: false
    [debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=in.amazon.mShop.android.shopping:id/sign_in_button]
    [debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":{"ELEMENT":"2"}}
    [debug] [AndroidBootstrap] Received command result from bootstrap
    [debug] [MJSONWP] Responding to client with driver.findElement() result: {"ELEMENT":"2"}
    [info] [HTTP] <-- POST /wd/hub/session/fb4c547d-3a81-4b48-b6ff-cb14eb629138/element 200 28 ms - 87 

等了60秒:

waited 60 seconds for a command
[debug] [AndroidDriver] Shutting down Android driver

Appium服务器在1分钟后关闭

2 个答案:

答案 0 :(得分:0)

默认的appium new命令超时值是60秒,这就是appium服务器在60秒后关闭驱动程序的原因。您可以通过设置以下功能来更改默认超时:

capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 6000);

capabilities.setCapability("newCommandTimeout", 6000);

答案 1 :(得分:0)

首先,Java中的NullPointerException仅在其引用的对象为null时才会出现。

因此,您在这种情况下使用的变量可能是null并且您正在对其执行某些调用,例如:

element.click ();

此处元素可能为null

所以你需要优雅地处理这种情况。如果元素为null,请不要点击它,而是记录错误并退出。