Appium 求助:怎么定位 HTML5 内的元素

wuming · 2014年12月03日 · 最后由 wuming 回复于 2014年12月05日 · 1546 次阅读

用了 N 种方法都没有能够定位到。
先转到 webview 模式。

public void SwitchtoWebViewModel(AppiumDriver driver) throws Exception {
    try {
        Set<String> contextNames = driver.getContextHandles();
        for (String contextName : contextNames) {
            System.out.println(contextName);
            // 用于返回被测app是NATIVE_APP还是WEBVIEW,如果两者都有就是混合型App
            if (contextName.contains("WEBVIEW")) {
                driver.context(contextName);
                // appDriver.switchTo().window("WEBVIEW");
                break;
            }// end if
        }// end for

然后用 xpath,id,class 定位均告失败。

            SwitchtoWebViewModel(driver);
Sleeper.sleep();
driver.findElementByXPath("//div[@class='More']").click();

结果报错。

An unknown server-side error occurred while processing the command. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 30.25 seconds
Build info: version: '2.42.2', revision: '6a6995d31c7c56c340d6f45a76976d43506cd6cc', time: '2014-06-03 10:52:47'
System info: host: 'wangjunbin', ip: '192.168.174.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_67'
Session ID: 66c22069-a7f7-47ae-93b2-e72331108ba0
Driver info: boc.framework.appium.test.AppiumDriverTestCase$AppiumSwipeableWebDriver
Capabilities [{platform=LINUX, app=F:/Test/Eclipse/Android4EZDB/src/main/resources/Apps/EZDBpro.apk, appActivit  y=com.boc.directbank.activities.WelcomeActivity, javascriptEnabled=true, browserName=Android, networkConnectionEnabled=true, desired={platformVersion=4.2.1, app=F:/Test/Eclipse/Android4EZDB/src/main/resources/Apps/EZDBpro.apk, appActivit  y=com.boc.directbank.activities.WelcomeActivity, platformName=Android, deviceName=Android Emulator, device=Android, appPackage=com.ss.boc.direct.bank.activity}, locationContextEnabled=false, appPackage=com.ss.boc.direct.bank.activity, platformVersion=4.2.1, databaseEnabled=false, deviceName=Android Emulator, platformName=Android, webStorageEnabled=false, device=Android, warnings={}, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:95)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:449)
    at test.boc.smoke.finance.testFinance.toFinance(testFinance.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

大神们,有没有办法定位 HTML5 元素的!!

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 16 条回复 时间 点赞

以后把 appium server 日志一块贴出来啊

@woshizh capabilities 里面 platfrom 改成 selendroid 吧

@luis 我试下啊

我来试试 4.2 的用 selendroid 能不能切换到 webview

我之前用的是 4.3~~现在用的是 4.2.。。。

@woshizh @luis 我试了 4.2 使用 selendroid 可以的

@panzhigang 转换以后 你是怎么处理 h5 中的数据呢? 比如是很多文字,你怎么取出来这些文字呢?

@gaoxing200851 跟 web 的一样啊

@panzhigang 那你是通过接口查看界面的元素吗?还是通过什么?

wuming #11 · 2014年12月05日 Author
multiple: false
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":13}
> info: [debug] Pushing command to appium work queue: ["find",{"strategy":"xpath","selector":"//div[@id='Wrap1']/div[@class='right']/p[@class='More']","context":"","multiple":false}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//div[@id='Wrap1']/div[@class='right']/p[@class='More']","context":"","multiple":false}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: find
> info: [debug] [BOOTSTRAP] [debug] Finding //div[@id='Wrap1']/div[@class='right']/p[@class='More'] using XPATH with the contextId:  multiple: false
wuming #12 · 2014年12月05日 Author

@luis @panzhigang 改成 selendroid 一样的报错,,,我将 xpath 写得清楚的不能再清楚了,还是识别不到。
//div[@id='Wrap1']/div[@class='right']/p[@class='More']。。
请问还有什么办法没?拜谢。

wuming #13 · 2014年12月05日 Author

@ 全体大神

@woshizh 如果是链接这样的可以试试 findElementByLinkText

wuming #15 · 2014年12月05日 Author

@panzhigang 它是一个 webkit 的页面,没有链接~

wuming #16 · 2014年12月11日 Author

4.4.2 的手机可以直接定位到 HTML5 的元素,问题算是暂时解决了

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册