Webelement.click() 在 appium 中给出 java.lang.NullPointerException [英] Webelement.click() giving java.lang.NullPointerException in appium

查看:29
本文介绍了Webelement.click() 在 appium 中给出 java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我尝试在代码行 element.click() 处运行下面的代码时,我都会收到 NullPointerException

注意:如果我评论最后一行代码,它就会通过.在

此外,该元素不为空,如下面的屏幕截图所示:-

我什至把 thread.sleep() 放在了以防万一是因为加载.findelementbyid() 方法中给出的 ID 是正确的.它还启动应用程序,然后它

它启动应用程序,然后抛出以下错误消息:-

java.lang.NullPointerException在 org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:279)在 org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:83)在 Appiumcapabilities.TestAppium(Appiumcapabilities.java:34)在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在 sun.reflect.NativeMethodAccessorImpl.invoke(来源不明)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(来源不明)在 java.lang.reflect.Method.invoke(Unknown Source)在 org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)在 org.testng.internal.Invoker.invokeMethod(Invoker.java:580)在 org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)

我正在使用 selenium-java-3.9.1 和 appium 服务器 1.7.1,testNGWindows 10,appium-java-client 版本 4.1.0

解决方案

不要显式设置 selenium 依赖appium-java-client 依赖项已经内置:您遇到了库不兼容问题.

如果需要修改Selenium的版本,可以像下面这样配置pom.xml:

<groupId>io.appium</groupId><artifactId>java-client</artifactId><version>${version.you.require}</version><范围>测试</测试><排除事项><排除><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId></排除></排除项></依赖><依赖><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>${selenium.version.you.require}</version></dependency>

同样的事情可以用 Gradle 来完成:

repositories {jcenter()行家{网址http://repo.maven.apache.org/maven2"}}依赖{...testCompile 组:'io.appium',名称:'java-client',版本:requiredVersion {排除模块:'selenium-java'}testCompile 组:'org.seleniumhq.selenium',名称:'selenium-java',版本:requiredSeleniumVersion...} 

I am getting NullPointerException everytime I try to run below code at code line element.click()

Note : It passes if I comment the last line of code. Also similar question was asked here but it didn't help.

AndroidDriver driver;
@Test
public void TestAppium() throws Exception{

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "MotoG5s Plus");
    capabilities.setCapability("platformVersion", "7.1.1");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("noReset", "true");

    File file = new File("D:\\Appium Workspace\\AppiumDemo\\apk\\MakeMyTrip Flights Hotels Cabs IRCTC Rail Bookings_v7.3.2_apkpure.com.apk");

    capabilities.setCapability("app", file.getAbsolutePath());

    driver = new AndroidDriver(new URL("http://10.80.196.55:4723/wd/hub"), capabilities);
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    Thread.sleep(10000);
    WebElement element = driver.findElementById("com.makemytrip:id/my_profile_icon");
    element.click();

}

Also my driver onject is not null as you can see from below screenshot

Also the element is not null as you can see in screenshot below :-

I have even put the thread.sleep() in case it is because of loading. The ID given in findelementbyid() method is correct. And also it launches the app and then it

It Launches the app and then it throws below error message :-

java.lang.NullPointerException
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:279)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:83)
at Appiumcapabilities.TestAppium(Appiumcapabilities.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)

I am using selenium-java-3.9.1 and appium server 1.7.1, testNG Windows 10, appium-java-client version 4.1.0

解决方案

Do not set selenium dependency explicitly as appium-java-client dependency already has it in-built: you are running into library incompatibility issue.

If it is necessary to change the version of Selenium then you can configure pom.xml like following:

<dependency>
  <groupId>io.appium</groupId>
  <artifactId>java-client</artifactId>
  <version>${version.you.require}</version>
  <scope>test</test>
  <exclusions>
    <exclusion>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>${selenium.version.you.require}</version>
</dependency>

Same thing can be done with Gradle:

repositories {
    jcenter()
    maven {
        url "http://repo.maven.apache.org/maven2"
    }
}

dependencies {
   ...
   testCompile group: 'io.appium', name: 'java-client', version: requiredVersion {
       exclude module: 'selenium-java'
   }
   
   testCompile group: 'org.seleniumhq.selenium', name: 'selenium-java', 
   version: requiredSeleniumVersion
   ...
}   

这篇关于Webelement.click() 在 appium 中给出 java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆