使用“ @AndroidFindBy()”定位元素时获取“ NoSuchElementException”

时间:2019-05-08 06:28:09

标签: java selenium appium-android

设置为: Android模拟器版本:Nexus 5X API 27,Android 8.1(Google Play)和浏览器:Google Chrome

我的声明如下:

    @AndroidFindBy(id = "user_login")
    WebElement user_login;

    @AndroidFindBy(id = "user_pass")
    WebElement user_pass;

    @AndroidFindBy(id = "wp-submit")
    WebElement wp_submit;

    String url = "http://demosite.center/wordpress/wp-login.php";

HTML:

<input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="Log In">

使用WebElements的方法

    public void enter(String usr, String pwd) {
        try {
            user_login.sendKeys(usr);
            System.out.println("entered username");
            Thread.sleep(2000);

            user_pass.sendKeys(pwd);
            System.out.println("entered password");
            Thread.sleep(2000);

            wp_submit.click();
            System.out.println("clicked login button");
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }

我得到的例外是:

[RemoteTestNG] detected TestNG version 6.14.3
May 08, 2019 11:31:22 AM io.appium.java_client.remote.AppiumCommandExecutor$1 lambda$0
INFO: Detected dialect: OSS
entered username
entered password
org.openqa.selenium.NoSuchElementException: Can't locate an element by this strategy: by id or name "wp_submit"
    at io.appium.java_client.pagefactory.AppiumElementLocator.findElement(AppiumElementLocator.java:126)
    at io.appium.java_client.pagefactory.interceptors.InterceptorOfASingleElement.intercept(InterceptorOfASingleElement.java:60)
    at io.appium.java_client.android.AndroidElement$$EnhancerByCGLIB$$b598166c.click(<generated>)
    at com.pages.DemoSiteWpLoginPage.enter(DemoSiteWpLoginPage.java:41)
    at com.tests.LoginTest.demoAutTest(LoginTest.java:44)
    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:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: org.openqa.selenium.TimeoutException: Expected condition failed: waiting for io.appium.java_client.pagefactory.AppiumElementLocator$WaitingFunction@3e2059ae (tried for 1 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.FluentWait.timeoutException(FluentWait.java:315)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:283)
    at io.appium.java_client.pagefactory.AppiumElementLocator.waitFor(AppiumElementLocator.java:99)
    at io.appium.java_client.pagefactory.AppiumElementLocator.findElement(AppiumElementLocator.java:119)
    ... 29 more
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"name","selector":"wp_submit"}
  (Session info: chrome=61.0.3163.98)
  (Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 10.0.17763 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z'
System info: host: 'M5-D-BZCJQM2', ip: '10.222.46.14', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities {browserName: Chrome, databaseEnabled: false, desired: {browserName: Chrome, deviceName: emulator-5554, newCommandTimeout: 100, platformName: Android, platformVersion: 8.1}, deviceManufacturer: Google, deviceModel: Android SDK built for x86, deviceName: emulator-5554, deviceScreenSize: 1080x1920, deviceUDID: emulator-5554, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: true, newCommandTimeout: 100, platform: LINUX, platformName: Android, platformVersion: 8.1.0, takesScreenshot: true, warnings: {}, webStorageEnabled: false}
Session ID: fb0e8048-7572-4f4d-bc07-b5ac0a219c08
*** Element info: {Using=name, value=wp_submit}
    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:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:231)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:322)
    at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:62)
    at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:400)
    at io.appium.java_client.DefaultGenericMobileDriver.findElementByName(DefaultGenericMobileDriver.java:118)
    at io.appium.java_client.AppiumDriver.findElementByName(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.findElementByName(AndroidDriver.java:1)
    at org.openqa.selenium.By$ByName.findElement(By.java:284)
    at org.openqa.selenium.support.ByIdOrName.findElement(ByIdOrName.java:50)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:314)
    at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:58)
    at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
    at io.appium.java_client.pagefactory.bys.ContentMappedBy.findElement(ContentMappedBy.java:50)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:314)
    at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:58)
    at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
    at io.appium.java_client.pagefactory.AppiumElementLocator.lambda$0(AppiumElementLocator.java:120)
    at io.appium.java_client.pagefactory.AppiumElementLocator$WaitingFunction.apply(AppiumElementLocator.java:172)
    at io.appium.java_client.pagefactory.AppiumElementLocator$WaitingFunction.apply(AppiumElementLocator.java:1)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:260)
    ... 31 more
PASSED: demoAutTest

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

我的假设:该脚本使用WebElement的变量名在UI上定位该元素,而不是我在@AndroidFindBy中传递的定位器。

请告知我是否需要我提供更多信息。

编辑1:

@AndroidFindBy(id = "user_login")
WebElement userName;

以上操作无效

@AndroidFindBy(id = "user_login")
WebElement user_login;

将变量名更改为id的名称后,上面的代码就可以正常工作

在此处发布问题之前,我已经在google上引用了以下引用,但是它们对@AndroidFindBy的实现略有不同,或者没有使用此方法。

How to fix "Can't locate an element by this strategy: Locator map:"

https://discuss.appium.io/t/nosuchelementexception-when-element-is-actually-present/18711/30

1 个答案:

答案 0 :(得分:2)

按照https://docs.microsoft.com/en-us/previous-versions/azure/ad/graph/api/entity-and-complex-type-reference#approleassignment-entity

  

用于标记页面对象上的字段,以指示用于定位元素或元素列表的替代机制。与PageFactory结合使用,可以使用户快速轻松地创建PageObjects。 使用Android UI选择器,可访问性,ID,名称,类名称,标记和xpath

这意味着您可以使用此注释来访问 Android UI元素,例如浏览器的“后退”和“重新加载”按钮

鉴于您要在Chrome浏览器中自动化网页,应将所有出现的@AndroidFindBy替换为@AndroidFindBy annotation documentation

更多信息: