Appium Test中的NoSuchElementException。无法正确获取定位器

时间:2017-10-26 13:03:27

标签: android automation appium

我在下面的链接中附加了Jave代码,错误日志,Appium错误日志和屏幕截图。

测试代码:

public static void main(String[] args ) throws MalformedURLException{
    WebDriver driver;
    //Set up desired capabilities and pass the Android app-activity and app-package to Appium
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("BROWSER_NAME","Android");
    capabilities.setCapability("VERSION","7.0"); 
    capabilities.setCapability("deviceName","ZY223ZMPG6");
    capabilities.setCapability("platformName","Android");
    capabilities.setCapability("noReset","false");

   capabilities.setCapability("appPackage","com.equationswork.kinghoo");
   // This package name of your app (you can get it from apk info app)
    capabilities.setCapability("appActivity","com.equationswork.kinghoo.MainActivity"); // This is Launcher activity of your app (you can get it from apk info app)
   //Create RemoteWebDriver instance and connect to the Appium server
   //It will launch the  App in Android Device using the configurations specified in Desired Capabilities
   driver = new RemoteWebDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
    //locate the Text on the  by using By.id()
   driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
   driver.findElement(MobileBy.id("com.android.packageinstaller:id/permission_allow_button")).click();
   driver.findElement(MobileBy.id("com.android.packageinstaller:id/permission_allow_button")).click();
   driver.findElement(MobileBy.id("com.android.packageinstaller:id/permission_allow_button")).click();
   driver.findElement(MobileBy.id("com.android.packageinstaller:id/permission_allow_button")).click();
   driver.findElement(By.xpath("//android.view.View[@index='0']/android.widget.EditText[@index='0']")).sendKeys("san.kktikam@eab.com");
   driver.findElement(By.xpath("//android.view.View[@index='1']/android.widget.EditText[@index='0']")).sendKeys("A3DGURK7");
   driver.findElement(By.id("btnLogin")).click();
  }

错误:

Oct 26, 2017 3:57:25 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 366.46 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'ADMIN-PC', ip: '192.168.56.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_151'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities [{appPackage=com.equationswork.kinghoo, deviceScreenSize=1080x1920, networkConnectionEnabled=true, noReset=false, warnings={}, databaseEnabled=false, deviceName=ZY223ZMPG6, platform=LINUX, deviceUDID=ZY223ZMPG6, appActivity=com.equationswork.kinghoo.MainActivity, desired={appPackage=com.equationswork.kinghoo, appActivity=com.equationswork.kinghoo.MainActivity, noReset=false, BROWSER_NAME=Android, VERSION=7.0, platformName=Android, deviceName=ZY223ZMPG6}, BROWSER_NAME=Android, platformVersion=7.0, webStorageEnabled=false, locationContextEnabled=false, VERSION=7.0, takesScreenshot=true, javascriptEnabled=true, deviceModel=Moto G (5) Plus, platformName=Android, deviceManufacturer=motorola}]
Session ID: f981bf89-4b7f-40e6-8efc-bd1a814b4eee
*** Element info: {Using=xpath, value=//android.view.View[@index='0']/android.widget.EditText[@index='0']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:509)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402)
    at com.appium.test.KingHoo.main(KingHoo.java:38)

Appium Server出错:

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.android.packageinstaller:id/permission_allow
_button' using 'ID' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.android.pac
kageinstaller:id/permission_allow_button]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":{"ELEMENT":"4"}}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [MJSONWP] Responding to client with driver.findElement() result: {"ELEMENT":"4"}
[HTTP] <-- POST /wd/hub/session/f981bf89-4b7f-40e6-8efc-bd1a814b4eee/element 200 599 ms - 87
[HTTP] --> POST /wd/hub/session/f981bf89-4b7f-40e6-8efc-bd1a814b4eee/element/4/click {"id":"4"}
[debug] [MJSONWP] Calling AppiumDriver.click() with args: ["4","f981bf89-4b7f-40e6-8efc-bd1a814b4eee
"]
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"element:click","par
ams":{"elementId":"4"}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"element:c
lick","params":{"elementId":"4"}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: click
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":true}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [MJSONWP] Responding to client with driver.click() result: true
[HTTP] <-- POST /wd/hub/session/f981bf89-4b7f-40e6-8efc-bd1a814b4eee/element/4/click 200 3128 ms - 7
6
[HTTP] --> POST /wd/hub/session/f981bf89-4b7f-40e6-8efc-bd1a814b4eee/element {"using":"xpath","value
":"//android.view.View[@index='0']/android.widget.EditText[@index='0']"}
[debug] [MJSONWP] Calling AppiumDriver.findElement() with args: ["xpath","//android.view.View[@index
='0']/android.widget.EditText[@index='0']","f981bf89-4b7f-40e6-8efc-bd1a814b4eee"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility
 id, -android uiautomator
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility
 id, -android uiautomator
[debug] [BaseDriver] Waiting up to 4000 ms for condition
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"st
rategy":"xpath","selector":"//android.view.View[@index='0']/android.widget.EditText[@index='0']","co
ntext":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","pa
rams":{"strategy":"xpath","selector":"//android.view.View[@index='0']/android.widget.EditText[@index
='0']","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding '//android.view.View[@index='0']/android.widget.E
ditText[@index='0']' using 'XPATH' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":7,"value":"Could not find an
element using supplied strategy. "}
[debug] [AndroidBootstrap] Received command result from bootstrap
[HTTP] <-- POST /wd/hub/session/f981bf89-4b7f-40e6-8efc-bd1a814b4eee/element 500 366437 ms - 164
[debug] [AndroidBootstrap] Emitting alert message...
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Emitting system alert message

1 个答案:

答案 0 :(得分:1)

我认为你先检查你的Xpath是否存在。为此我写了一个功能很好的函数。试试这个功能。

public static String checkpathispresent(String strXpath)
{
    try 
   {
      String lead;
      Object elem = driver.findElementByXPath(strXpath);
      if(elem.equals(null))  
      {
          lead="";
          System.out.println(strXpath+ "has no value so setting blank.");  

      }
      else 
      {
        lead = driver.findElementByXPath(strXpath).getText();
        System.out.println("lead is "+lead);

      }
      return lead;

    }catch(Exception ex)
    {
       System.out.println("Xpath do not exist");
       return "";

    }

}

另外针对您的问题尝试使用此xpath - “//android.view.View[@index='2']/android.view.View[@index='0']/android.widget.EditText[@index='0']”你的Xpath必须是唯一的这样它就可以访问,不会与其他元素的Xpath冲突。在您的情况下,索引2的视图根据您的屏幕截图具有0-8内部视图。