Appium 切换到 webview 后使用 click 方法未抛异常但模拟器未点击

cloudy · September 06, 2016 · Last by 美好应许 replied at August 20, 2018 · 1401 hits

环境:
PLATFORM_VERSION:夜神模拟器,安卓 4.4.2
appium 版本:1.4.16.1


在一个 webview 里面找到了 add-btn 这个按钮,但是它为什么不点击呢?
testng 显示是通过的,但是模拟器里面实际是没有点击的。

(大神可以下载我的 app 试运行一下我的代码)对应 app(云之家)下载地址:enter link description here
下面是 click 事件附近的 appium 日志,未发现报异常。

> info: [debug] Responding to client with success: {"status":0,"value":"WEBVIEW_com.kdweibo.client","sessionId":"24451f0b-0d44-4ece-8a4e-f4c80fc0331c"}
> info: <-- GET /wd/hub/session/24451f0b-0d44-4ece-8a4e-f4c80fc0331c/context 200 1.365 ms - 100 {"status":0,"value":"WEBVIEW_com.kdweibo.client","sessionId":"24451f0b-0d44-4ece-8a4e-f4c80fc0331c"}
> info: --> POST /wd/hub/session/24451f0b-0d44-4ece-8a4e-f4c80fc0331c/element {"using":"id","value":"add-btn"}
> info: JSONWP Proxy: Proxying [POST /wd/hub/session/24451f0b-0d44-4ece-8a4e-f4c80fc0331c/element] to [POST http://127.0.0.1:9515/wd/hub/session/8be42b64518dc90e37af4b7bf81a70ec/element] with body: {"using":"id","value":"add-btn"}
> info: JSONWP Proxy: Got response with status 200: {"sessionId":"8be42b64518dc90e37af4b7bf81a70ec","status":0,"value":{"ELEMENT":"0.7036180328577757-1"}}
> info: JSONWP Proxy: Replacing sessionId 8be42b64518dc90e37af4b7bf81a70ec with 24451f0b-0d44-4ece-8a4e-f4c80fc0331c
> info: <-- POST /wd/hub/session/24451f0b-0d44-4ece-8a4e-f4c80fc0331c/element 200 206.830 ms - 106 
> info: --> POST /wd/hub/session/24451f0b-0d44-4ece-8a4e-f4c80fc0331c/element/0.7036180328577757-1/click {"id":"0.7036180328577757-1"}
> info: JSONWP Proxy: Proxying [POST /wd/hub/session/24451f0b-0d44-4ece-8a4e-f4c80fc0331c/element/0.7036180328577757-1/click] to [POST http://127.0.0.1:9515/wd/hub/session/8be42b64518dc90e37af4b7bf81a70ec/element/0.7036180328577757-1/click] with body: {"id":"0.7036180328577757-1"}
> info: JSONWP Proxy: Got response with status 200: {"sessionId":"8be42b64518dc90e37af4b7bf81a70ec","status":0,"value":null}
> info: JSONWP Proxy: Replacing sessionId 8be42b64518dc90e37af4b7bf81a70ec with 24451f0b-0d44-4ece-8a4e-f4c80fc0331c
> info: <-- POST /wd/hub/session/24451f0b-0d44-4ece-8a4e-f4c80fc0331c/element/0.7036180328577757-1/click 200 168.114 ms - 76 
> info: --> POST /wd/hub/session/24451f0b-0d44-4ece-8a4e-f4c80fc0331c/element {"using":"id","value":"add-btn"}
> info: JSONWP Proxy: Proxying [POST /wd/hub/session/24451f0b-0d44-4ece-8a4e-f4c80fc0331c/element] to [POST http://127.0.0.1:9515/wd/hub/session/8be42b64518dc90e37af4b7bf81a70ec/element] with body: {"using":"id","value":"add-btn"}
> info: JSONWP Proxy: Got response with status 200: {"sessionId":"8be42b64518dc90e37af4b7bf81a70ec","status":0,"value":{"ELEMENT":"0.7036180328577757-1"}}
> info: JSONWP Proxy: Replacing sessionId 8be42b64518dc90e37af4b7bf81a70ec with 24451f0b-0d44-4ece-8a4e-f4c80fc0331c
> info: <-- POST /wd/hub/session/24451f0b-0d44-4ece-8a4e-f4c80fc0331c/element 200 49.692 ms - 106 
> info: --> GET /wd/hub/session/24451f0b-0d44-4ece-8a4e-f4c80fc0331c/element/0.7036180328577757-1/displayed {}
> info: JSONWP Proxy: Proxying [GET /wd/hub/session/24451f0b-0d44-4ece-8a4e-f4c80fc0331c/element/0.7036180328577757-1/displayed] to [GET http://127.0.0.1:9515/wd/hub/session/8be42b64518dc90e37af4b7bf81a70ec/element/0.7036180328577757-1/displayed] with body: {}
> info: JSONWP Proxy: Got response with status 200: {"sessionId":"8be42b64518dc90e37af4b7bf81a70ec","status":0,"value":true}
> info: JSONWP Proxy: Replacing sessionId 8be42b64518dc90e37af4b7bf81a70ec with 24451f0b-0d44-4ece-8a4e-f4c80fc0331c
> info: <-- GET /wd/hub/session/24451f0b-0d44-4ece-8a4e-f4c80fc0331c/element/0.7036180328577757-1/displayed 200 65.589 ms - 76 
> info: --> DELETE /wd/hub/session/24451f0b-0d44-4ece-8a4e-f4c80fc0331c {}
> info: Shutting down appium session
> info: [debug] Pressing the HOME button
> info: [debug] executing cmd: C:\Users\kingdee\AppData\Local\Android\android-sdk\platform-tools\adb.exe -s 127.0.0.1:62001 shell "input keyevent 3"
> info: [debug] Stopping logcat capture

package com.yzj.liaghtapp;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;

public class customermanage {
     private AndroidDriver<AndroidElement> driver;
        private WebDriverWait wait;

         @BeforeClass
        public void setUp() throws Exception {
            File classpathRoot = new File(System.getProperty("user.dir"));
            File appDir = new File(classpathRoot, "app");
            File app = new File(appDir, "云之家.apk");
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "127.0.0.1:62001"); 
            capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
            capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
            capabilities.setCapability(MobileCapabilityType.APP_PACKAGE, "com.kdweibo.client");
            driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        }

        @AfterClass(alwaysRun = true)
        public void tearDown() throws Exception {
            driver.quit();
        }

        @Test
        public void waitLaunch(){
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(driver.findElement(By.id("login")).isDisplayed()){
                driver.findElement(By.id("login")).click();
            }
            wait = new WebDriverWait(driver,30);
            wait.until(ExpectedConditions.presenceOfElementLocated(By.id("lg_portrait_border")));
        }
        @Test(dependsOnMethods = "waitLaunch")
        public void login(){
            driver.findElement(By.id("et_number")).sendKeys("13296884163");
            driver.findElement(By.id("inputPassword")).click();
            driver.findElement(By.id("inputPassword")).sendKeys("hxnearc228");
            driver.hideKeyboard();
            driver.findElement(By.name("登录")).click();
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(driver.findElement(By.name("完善个人信息")).isDisplayed()){
                driver.findElement(By.id("btn_right")).click();//出现完善个人信息点击下一步
            }
            wait.until(ExpectedConditions.presenceOfElementLocated(By.id("tv_title")));
        }
        @Test(dependsOnMethods = "login")
        public void switchteam(){
            driver.findElement(By.name("我")).click();
            driver.findElement(By.id("btn_popup")).click();
            driver.findElement(By.name("测试大本营")).click();
            wait.until(ExpectedConditions.presenceOfElementLocated(By.id("tv_title")));
        }
        @Test(dependsOnMethods = "switchteam")
        public void openlightapp(){
            driver.findElement(By.name("应用")).click();
               try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
               if(driver.findElement(By.name("长按图标进行编辑操作")).isDisplayed())
               {
                   driver.findElement(By.id("im_tips_dissmiss")).click();//关闭引导
               }
               driver.findElement(By.name("商机管理(模拟)")).click();
               try {
                Thread.sleep(10000);//等待webview加载,必须要给足时间
               } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
               }
             //遍历context,切换到webview,注意 有些app可能有多个webview
               Set<String> context = driver.getContextHandles();
               for(String contextname : context){
               System.out.println(contextname);//打印
               if(contextname.contains("WEBVIEW"))
               driver.context(contextname);
               }
               String url = driver.getCurrentUrl();//获取webview的url,native是获取不到会出错的
               String pageSource=driver.getPageSource();
               boolean ispageSourceEmpty=(pageSource ==null || pageSource.isEmpty());
               System.out.println(driver.getPageSource());
               System.out.println(url);
               //直接获取web的元素
               System.out.println("contextname:!!"+driver.getContext());
               Assert.assertEquals(ispageSourceEmpty, false);

        }
        @Test(dependsOnMethods = "openlightapp")
        public void businessOpportunitiesmain(){
            driver.findElement(By.id("add-btn")).click();
            System.out.println("add-btn isdisplayed?:"+driver.findElement(By.id("add-btn")).isDisplayed());

        }


}


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

我也有同样的问题😓

一样的问题,你看看按钮绑定的事件是不是 touchstart 事件

小珍 回复

自己封装了一套自动获取元素坐标方法,然后通过点击坐标解决了,貌似我们的 webview 都没有 click 事件的,都是 tap 事件。

确实是因为按钮只有 tap 事件,没有 click,所以不能 click,坑了我好久。。

cloudy 回复

亲,你获取的 webview 元素坐标是正确的吗?能不能贴下相关代码?

Author only
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up