org.openqa.selenium.NoSuchElementException 尝试使用 Appium 自动化我的应用程序
Getting org.openqa.selenium.NoSuchElementException on trying to automate my app using Appium
在您将我的问题标记为重复之前,请理解我已经关注了 Whosebug 上的所有类似主题,包括:
org.openqa.selenium.NoSuchElementException: no such element
How to resolve org.openqa.selenium.NoSuchElementException in Selenium?
org.openqa.selenium.NoSuchElementException
org.openqa.selenium.NoSuchElementException Cannot find textbox element.
然而,尽管我已经尝试解决了 2 天,但还是无法解决问题。
我将 Eclipse 与 TestNG、Appium 和 uiautomatorviewer 结合使用来查看我的应用程序的布局详细信息。
这是我的基础代码 class:
public class BaseClass {
static AppiumDriver<MobileElement> driver;
@BeforeTest
public void setup() throws Exception
{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "ANDROID");
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1.2");
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Redmi");
caps.setCapability(MobileCapabilityType.UDID, "6f13c427d440");
caps.setCapability("appPackage", "com.example.shipmart");
caps.setCapability("appActivity", "com.example.shipmart.MainActivity");
caps.setCapability("autoGrantPermissions", true);
caps.setCapability(MobileCapabilityType.APP, "C:\Users\Sparsh\eclipse-workspace\meriArt\src\test\resources\apps\meri.apk" );
caps.setCapability("automationName", "uiautomator2");
URL url = new URL("http://localhost:4723/wd/hub");
driver = new AppiumDriver<MobileElement>(url, caps);
}
@AfterSuite
public void teardown()
{
driver.close();
driver.quit();
}
}
这是我使用 TestNG 测试 class 的代码:
public class Tests extends BaseClass {
@Test
public void testOne()
{
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
MobileElement etSalary =driver.findElement(By.id("com.example.shipmart:id/etSalary"));
etSalary.setValue("45555"); //THIS WORKS FINE
MobileElement btnGo =driver.findElement(By.id("com.example.shipmart:id/btnGo"));
//EXCEPTION OCCURS IN ABOVE LINE
btnGo.click();
System.out.println("ID of ImageButton is: " + btnGo.getId());
}
}
正在检测第一个元素 'etSalary' 并用提供的值填充,但在尝试定位第二个元素 btnGo 时出现异常。
这是完整的错误信息:
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'SPARSH', ip: '192.168.1.229', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_261'
Driver info: io.appium.java_client.AppiumDriver
Capabilities {app: C:\Users\Sparsh\eclipse-wor..., appActivity: com.example.shipmart.MainAc..., appPackage: com.example.shipmart, autoGrantPermissions: true, automationName: uiautomator2, databaseEnabled: false, desired: {app: C:\Users\Sparsh\eclipse-wor..., appActivity: com.example.shipmart.MainAc..., appPackage: com.example.shipmart, autoGrantPermissions: true, automationName: uiautomator2, deviceName: Redmi, platformName: android, platformVersion: 7.1.2, udid: 6f13c427d440}, deviceApiLevel: 25, deviceManufacturer: Xiaomi, deviceModel: Redmi 4, deviceName: 6f13c427d440, deviceScreenDensity: 320, deviceScreenSize: 720x1280, deviceUDID: 6f13c427d440, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: true, pixelRatio: 2, platform: LINUX, platformName: ANDROID, platformVersion: 7.1.2, statBarHeight: 50, takesScreenshot: true, udid: 6f13c427d440, viewportRect: {height: 1230, left: 0, top: 50, width: 720}, warnings: {}, webStorageEnabled: false}
Session ID: f5c149f4-5da8-4923-953a-7cf404680701
*** Element info: {Using=id, value=com.example.shipmart:id/btnGo}
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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:239)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:41)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:61)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:372)
at io.appium.java_client.DefaultGenericMobileDriver.findElementById(DefaultGenericMobileDriver.java:69)
at io.appium.java_client.AppiumDriver.findElementById(AppiumDriver.java:1)
at org.openqa.selenium.By$ById.findElement(By.java:188)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:57)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at tests.Tests.testOne(Tests.java:25)
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:132)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:599)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:822)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.util.ArrayList.forEach(Unknown Source)
at org.testng.TestRunner.privateRun(TestRunner.java:764)
at org.testng.TestRunner.run(TestRunner.java:585)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1218)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.runSuites(TestNG.java:1069)
at org.testng.TestNG.run(TestNG.java:1037)
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)
我试过使用wait、xpath、classname、content-desc等来定位元素,将Java 10降级为Java 8,但我无法解决问题。
请帮忙!
显示此异常的原因可能有多种。
- 当你在前一个元素中发送键时,这个元素是否在屏幕上。
- 如果在向 prev 元素发送密钥后它获得启用或可见,那么您应该使用显式等待来处理它。或者您可以尝试在找到 buttongo 元素之前添加睡眠。
- Button go 元素可能位于不可见的屏幕中,您可能需要滚动 Android 页面并转到该元素。
希望对您有所帮助。
在您将我的问题标记为重复之前,请理解我已经关注了 Whosebug 上的所有类似主题,包括:
org.openqa.selenium.NoSuchElementException: no such element
How to resolve org.openqa.selenium.NoSuchElementException in Selenium?
org.openqa.selenium.NoSuchElementException
org.openqa.selenium.NoSuchElementException Cannot find textbox element.
然而,尽管我已经尝试解决了 2 天,但还是无法解决问题。
我将 Eclipse 与 TestNG、Appium 和 uiautomatorviewer 结合使用来查看我的应用程序的布局详细信息。
这是我的基础代码 class:
public class BaseClass {
static AppiumDriver<MobileElement> driver;
@BeforeTest
public void setup() throws Exception
{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "ANDROID");
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1.2");
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Redmi");
caps.setCapability(MobileCapabilityType.UDID, "6f13c427d440");
caps.setCapability("appPackage", "com.example.shipmart");
caps.setCapability("appActivity", "com.example.shipmart.MainActivity");
caps.setCapability("autoGrantPermissions", true);
caps.setCapability(MobileCapabilityType.APP, "C:\Users\Sparsh\eclipse-workspace\meriArt\src\test\resources\apps\meri.apk" );
caps.setCapability("automationName", "uiautomator2");
URL url = new URL("http://localhost:4723/wd/hub");
driver = new AppiumDriver<MobileElement>(url, caps);
}
@AfterSuite
public void teardown()
{
driver.close();
driver.quit();
}
}
这是我使用 TestNG 测试 class 的代码:
public class Tests extends BaseClass {
@Test
public void testOne()
{
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
MobileElement etSalary =driver.findElement(By.id("com.example.shipmart:id/etSalary"));
etSalary.setValue("45555"); //THIS WORKS FINE
MobileElement btnGo =driver.findElement(By.id("com.example.shipmart:id/btnGo"));
//EXCEPTION OCCURS IN ABOVE LINE
btnGo.click();
System.out.println("ID of ImageButton is: " + btnGo.getId());
}
}
正在检测第一个元素 'etSalary' 并用提供的值填充,但在尝试定位第二个元素 btnGo 时出现异常。
这是完整的错误信息:
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'SPARSH', ip: '192.168.1.229', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_261'
Driver info: io.appium.java_client.AppiumDriver
Capabilities {app: C:\Users\Sparsh\eclipse-wor..., appActivity: com.example.shipmart.MainAc..., appPackage: com.example.shipmart, autoGrantPermissions: true, automationName: uiautomator2, databaseEnabled: false, desired: {app: C:\Users\Sparsh\eclipse-wor..., appActivity: com.example.shipmart.MainAc..., appPackage: com.example.shipmart, autoGrantPermissions: true, automationName: uiautomator2, deviceName: Redmi, platformName: android, platformVersion: 7.1.2, udid: 6f13c427d440}, deviceApiLevel: 25, deviceManufacturer: Xiaomi, deviceModel: Redmi 4, deviceName: 6f13c427d440, deviceScreenDensity: 320, deviceScreenSize: 720x1280, deviceUDID: 6f13c427d440, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: true, pixelRatio: 2, platform: LINUX, platformName: ANDROID, platformVersion: 7.1.2, statBarHeight: 50, takesScreenshot: true, udid: 6f13c427d440, viewportRect: {height: 1230, left: 0, top: 50, width: 720}, warnings: {}, webStorageEnabled: false}
Session ID: f5c149f4-5da8-4923-953a-7cf404680701
*** Element info: {Using=id, value=com.example.shipmart:id/btnGo}
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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:239)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:41)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:61)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:372)
at io.appium.java_client.DefaultGenericMobileDriver.findElementById(DefaultGenericMobileDriver.java:69)
at io.appium.java_client.AppiumDriver.findElementById(AppiumDriver.java:1)
at org.openqa.selenium.By$ById.findElement(By.java:188)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:57)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at tests.Tests.testOne(Tests.java:25)
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:132)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:599)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:822)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.util.ArrayList.forEach(Unknown Source)
at org.testng.TestRunner.privateRun(TestRunner.java:764)
at org.testng.TestRunner.run(TestRunner.java:585)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1218)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.runSuites(TestNG.java:1069)
at org.testng.TestNG.run(TestNG.java:1037)
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)
我试过使用wait、xpath、classname、content-desc等来定位元素,将Java 10降级为Java 8,但我无法解决问题。
请帮忙!
显示此异常的原因可能有多种。
- 当你在前一个元素中发送键时,这个元素是否在屏幕上。
- 如果在向 prev 元素发送密钥后它获得启用或可见,那么您应该使用显式等待来处理它。或者您可以尝试在找到 buttongo 元素之前添加睡眠。
- Button go 元素可能位于不可见的屏幕中,您可能需要滚动 Android 页面并转到该元素。
希望对您有所帮助。