.click() is not working on android device or emulator

Hi, I am trying to automate a simple login page on chrome or android browser on real device/emulator. But .click() is not working. Tried using all below options but none worked

  • tried using WebElement/MobileElement click() method.
  • tried using javascript click.
  • tried using TouchAction tap
  • tried using javascript mobile:tap

Sample code is below and also attached the appium logs. This has blocked me terribly, can someone please let know what am i missing here. Note that same code works fine on desktop

code
public class TestMobileBrowser {
public DesiredCapabilities capabilities;
public RemoteWebDriver driver;

@BeforeTest
public void beforeTest() throws MalformedURLException{
    capabilities = new DesiredCapabilities();
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("deviceName", "Android");
    capabilities.setCapability("browserName", "Browser");
    capabilities.setCapability("newCommandTimeout", 60);
    driver = new AndroidDriver<WebElement>(new URL("http://localhost:4723/wd/hub"), capabilities);    
}

@Test
public void test() throws InterruptedException{

WebDriverWait wait = new WebDriverWait(driver, 30);    
driver.get("http://testurl");
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='userName']")));
driver.findElement(By.xpath("//input[@name='userName']")).sendKeys("testvk1");
driver.findElement(By.xpath("//input[@name='password']")).sendKeys("test123");
driver.findElement(By.xpath("//input[@name='password']")).sendKeys(Keys.TAB);
elementHighlight(driver.findElement(By.id("button-1041"))); //this method highlights the element correctly using javascript 

wait.until(ExpectedConditions.elementToBeClickable(By.id("button-1041")));
driver.findElement(By.id("button-1041")).click();

}

Appium Logs

info: → POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element {“using”:“xpath”,“value”:“//input[@name=‘password’]”}
info: JSONWP Proxy: Proxying [POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element] to [POST http://127.0.0.1:9515/wd/hub/session/23865a2b1a7d285514a579c17653139b/element] with body: {“using”:“xpath”,“value”:“//input[@name=‘password’]”}
info: JSONWP Proxy: Got response with status 200: {“sessionId”:“23865a2b1a7d285514a579c17653139b”,“status”:0,“value”:{“ELEMENT”:“0.30240034498274326-2”}}
info: JSONWP Proxy: Replacing sessionId 23865a2b1a7d285514a579c17653139b with 23865a2b1a7d285514a579c17653139b
info: ← POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element 200 804.649 ms - 103
info: → POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element/0.30240034498274326-2/value {“id”:“0.30240034498274326-2”,“value”:[“test123”]}
info: JSONWP Proxy: Proxying [POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element/0.30240034498274326-2/value] to [POST http://127.0.0.1:9515/wd/hub/session/23865a2b1a7d285514a579c17653139b/element/0.30240034498274326-2/value] with body: {“id”:“0.30240034498274326-2”,“value”:[“egain123”]}
info: JSONWP Proxy: Got response with status 200: {“sessionId”:“23865a2b1a7d285514a579c17653139b”,“status”:0,“value”:null}
info: JSONWP Proxy: Replacing sessionId 23865a2b1a7d285514a579c17653139b with 23865a2b1a7d285514a579c17653139b
info: ← POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element/0.30240034498274326-2/value 200 5613.754 ms - 72
info: → POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element {“using”:“xpath”,“value”:“//input[@name=‘password’]”}
info: JSONWP Proxy: Proxying [POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element] to [POST http://127.0.0.1:9515/wd/hub/session/23865a2b1a7d285514a579c17653139b/element] with body: {“using”:“xpath”,“value”:“//input[@name=‘password’]”}
info: JSONWP Proxy: Got response with status 200: {“sessionId”:“23865a2b1a7d285514a579c17653139b”,“status”:0,“value”:{“ELEMENT”:“0.30240034498274326-2”}}
info: JSONWP Proxy: Replacing sessionId 23865a2b1a7d285514a579c17653139b with 23865a2b1a7d285514a579c17653139b
info: ← POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element 200 140.445 ms - 103
info: → POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element/0.30240034498274326-2/value {“id”:“0.30240034498274326-2”,“value”:[“”]}
info: JSONWP Proxy: Proxying [POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element/0.30240034498274326-2/value] to [POST http://127.0.0.1:9515/wd/hub/session/23865a2b1a7d285514a579c17653139b/element/0.30240034498274326-2/value] with body: {“id”:“0.30240034498274326-2”,“value”:[“”]}
info: JSONWP Proxy: Got response with status 200: {“sessionId”:“23865a2b1a7d285514a579c17653139b”,“status”:0,“value”:null}
info: JSONWP Proxy: Replacing sessionId 23865a2b1a7d285514a579c17653139b with 23865a2b1a7d285514a579c17653139b
info: ← POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element/0.30240034498274326-2/value 200 1060.420 ms - 72
info: → POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element {“using”:“id”,“value”:“button-1041”}
info: JSONWP Proxy: Proxying [POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element] to [POST http://127.0.0.1:9515/wd/hub/session/23865a2b1a7d285514a579c17653139b/element] with body: {“using”:“id”,“value”:“button-1041”}
info: JSONWP Proxy: Got response with status 200: {“sessionId”:“23865a2b1a7d285514a579c17653139b”,“status”:0,“value”:{“ELEMENT”:“0.30240034498274326-3”}}
info: JSONWP Proxy: Replacing sessionId 23865a2b1a7d285514a579c17653139b with 23865a2b1a7d285514a579c17653139b
info: ← POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element 200 285.739 ms - 103
info: → POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/execute {“script”:“arguments[0].style.border=‘3px solid yellow’”,“args”:[{“ELEMENT”:“0.30240034498274326-3”,“element-6066-11e4-a52e-4f735466cecf”:“0.30240034498274326-3”}]}
info: JSONWP Proxy: Proxying [POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/execute] to [POST http://127.0.0.1:9515/wd/hub/session/23865a2b1a7d285514a579c17653139b/execute] with body: {“script”:“arguments[0].style.border=‘3px solid yellow’”,“args”:[{“ELEMENT”:“0.30240034498274326-3”,“element-6066-11e4-a52e-4f735466cecf”:“0.30240034498274326-3”}]}
info: JSONWP Proxy: Got response with status 200: {“sessionId”:“23865a2b1a7d285514a579c17653139b”,“status”:0,“value”:null}
info: JSONWP Proxy: Replacing sessionId 23865a2b1a7d285514a579c17653139b with 23865a2b1a7d285514a579c17653139b
info: ← POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/execute 200 64.399 ms - 72
info: → POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/execute {“script”:“arguments[0].style.border=‘’”,“args”:[{“ELEMENT”:“0.30240034498274326-3”,“element-6066-11e4-a52e-4f735466cecf”:“0.30240034498274326-3”}]}
info: JSONWP Proxy: Proxying [POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/execute] to [POST http://127.0.0.1:9515/wd/hub/session/23865a2b1a7d285514a579c17653139b/execute] with body: {“script”:“arguments[0].style.border=‘’”,“args”:[{“ELEMENT”:“0.30240034498274326-3”,“element-6066-11e4-a52e-4f735466cecf”:“0.30240034498274326-3”}]}
info: JSONWP Proxy: Got response with status 200: {“sessionId”:“23865a2b1a7d285514a579c17653139b”,“status”:0,“value”:null}
info: JSONWP Proxy: Replacing sessionId 23865a2b1a7d285514a579c17653139b with 23865a2b1a7d285514a579c17653139b
info: ← POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/execute 200 66.011 ms - 72
info: → POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element {“using”:“xpath”,“value”:“//span[text()=‘Sign In’]”}
info: JSONWP Proxy: Proxying [POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element] to [POST http://127.0.0.1:9515/wd/hub/session/23865a2b1a7d285514a579c17653139b/element] with body: {“using”:“xpath”,“value”:“//span[text()=‘Sign In’]”}
info: JSONWP Proxy: Got response with status 200: {“sessionId”:“23865a2b1a7d285514a579c17653139b”,“status”:0,“value”:{“ELEMENT”:“0.30240034498274326-4”}}
info: JSONWP Proxy: Replacing sessionId 23865a2b1a7d285514a579c17653139b with 23865a2b1a7d285514a579c17653139b
info: ← POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element 200 187.943 ms - 103
info: → POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element/0.30240034498274326-4/click {“id”:“0.30240034498274326-4”}
info: JSONWP Proxy: Proxying [POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element/0.30240034498274326-4/click] to [POST http://127.0.0.1:9515/wd/hub/session/23865a2b1a7d285514a579c17653139b/element/0.30240034498274326-4/click] with body: {“id”:“0.30240034498274326-4”}
info: JSONWP Proxy: Got response with status 200: {“sessionId”:“23865a2b1a7d285514a579c17653139b”,“status”:0,“value”:null}
info: JSONWP Proxy: Replacing sessionId 23865a2b1a7d285514a579c17653139b with 23865a2b1a7d285514a579c17653139b
info: ← POST /wd/hub/session/23865a2b1a7d285514a579c17653139b/element/0.30240034498274326-4/click 200 1059.527 ms - 72
info: [debug] Didn’t get a new command in 60 secs, shutting down…

I assume that you can click successfully (no error throw out) but there is no effect on the target element. and the behavior seems you are testing for an Hybrid-app. And that page has jQuery with “touchstart”.

(above is just my guess, please correct if I got something wrong)

Can you give a try using tap on location (co-ordinate)?

close the keyboard, then click on the submit button

I am facing the same issue :

ENVIORNMENT:
Appium version : 1.6.5 this i am using from command line.
Last Appium version that did not exhibit the issue (if applicable): v1.1.0-beta.4 (this is UI version) {This is with UI}
Desktop OS/version used to run Appium: Window 7 proffesional / 64 bit
Node.js version (unless using Appium.app|exe): Node.js 6.11.0 (x64) and npm
Mobile platform/version under test: 4.2.1
Real device or emulator/simulator: Real Device
Appium CLI or Appium.app|exe: Appium CLI

My Code:
`public class DragandDrop
{
RemoteWebDriver driver;

String device = "Android"; 
String devicename = "0123456789ABCDEF";
String platformversion = "4.2.1";
//String devicename = "f292d6fb7d63";
//String platformversion = "6.0.1";
String platformname = "Android";

@Test
public void getDesireCapabilities() throws MalformedURLException, InterruptedException
{
	
	
	DesiredCapabilities cap = new DesiredCapabilities();
	cap.setCapability("device", device);
	cap.setCapability("deviceName", devicename);
	cap.setCapability("platformName", platformname);
	cap.setCapability("platformVersion", platformversion);
	cap.setCapability("browserName", "");
	cap.setCapability("appPackage", "com.mobeta.android.demodslv");
	cap.setCapability("appActivity", "com.mobeta.android.demodslv.Launcher");
	cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "100");
	
	
	driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"),cap);
	
	System.out.println("Test1");
	driver.findElement(By.xpath("//android.widget.TextView[@text='Basic usage playground']")).click();
	System.out.println("Test2");
	`

ISSUE :

  1. When i Run this code in Android 4.2.1, It works well. App opened and driver clicks on the element.
  2. when i Run this code on Android 6.0.1(MI device), App will open up but driver will not able to click on element and even there is no error shown on eclipse.

As you can see that i used System.out.println(“Test1”); and System.out.println(“Test2”);

In Output both are coming Test1 and Test2, script shown successfull but in device element not clicked and not navigated to another page.

Please resolve/Guide.

You can watch this video: https://youtu.be/HnG4-PuKcL4

may be you will get answer of ur question.

1 Like

Same issue occurs me also, try below combination it will definitely work

Java Client 5.0.4
Appium Server 1.7.2
Selenium 3.9.1
Java jdk version 1.8.0_161
commons-lang3-3.7.jar

Hi All!
I was never able to make .click() or any of the other options work with my hybrid app. What did work for me was to tap X and Y coordinates while in the NATIVE_APP context. I ended up writing my own click/tap method that I use anytime I want to click an element found in the WebView

The general steps are:

  1. Find the element’s X,Y position in the WebView.
  2. Since the amount of pixels in the NATIVE_APP context is different than in the WebView Context, The element position will be different in the NATIVE_APP… a conversion must be done between the two
  3. Perform a DeviceAction tap with the converted coordinates in the Native context
  4. switch back to the WebView context for further testing.

Java:

public static void TapWebViewElement(WebElement el, AppiumDriver appDriver, ThreadLog threadLogger){
    try {
        appDriver.hideKeyboard();
    }catch (Exception e){
        // System.out.println("No Keyboard up " + e.getMessage());
    }
    Point pt = el.getLocation();
    Dimension size = el.getSize();
    int x = pt.getX() +(size.getWidth() / 2);
    int y = pt.getY() +  (size.getHeight() / 2);
   
 JavascriptExecutor js = (JavascriptExecutor) appDriver;
     
 int screenWebViewWidth = ((Long) js.executeScript("return window.innerWidth || document.body.clientWidth")).intValue();
    
  int screenWebViewHeight = ((Long) js.executeScript("return window.innerHeight || document.body.clientHeight")).intValue();

    //Scroll If Necessary
    if(y > screenWebViewHeight){
        js.executeScript("arguments[0].scrollIntoView(true);", el);
        try{Thread.sleep(500);}catch (InterruptedException e){}
        pt = el.getLocation();
        size = el.getSize();
        x = pt.getX() +(size.getWidth() / 2);
        y = pt.getY() +  (size.getHeight() / 2);
    }
    Set<String> contextNames = ((AppiumDriver) appDriver).getContextHandles();
    for (String contextName : contextNames) {
            if (contextName.contains("NATIVE")) {
                ((AppiumDriver<MobileElement>) appDriver).context(contextName);
                System.out.println(threadLogger.deviceName + " Switching to: " + contextName);

            }

    }
    double screenWidth = appDriver.manage().window().getSize().getWidth();
    double screenHeight = appDriver.manage().window().getSize().getHeight();

    double elementNativeViewX = (x * screenWidth) / screenWebViewWidth;
    double elementNativeViewY = (y * screenHeight) / screenWebViewHeight;
    threadLogger.info( " Real Tap X:"+elementNativeViewX + ", Real Tap Y:"+elementNativeViewY);
    try{Thread.sleep(1000);}catch(Exception e){};
    new TouchAction((MobileDriver) appDriver).tap((int)elementNativeViewX,(int)elementNativeViewY).perform();

    contextNames = ((AppiumDriver) appDriver).getContextHandles();
    for (String contextName : contextNames) {
        if(threadLogger.isAndroid) {
            if (contextName.contains("chromeview")) {
                ((AppiumDriver<MobileElement>) appDriver).context(contextName);
                // System.out.println("Switch To: " + contextName);
                System.out.println(threadLogger.deviceName + " Switching to: " + contextName);
            }
        }else {
                if (contextName.contains("WEBVIEW")) {
                  ((AppiumDriver<MobileElement>) appDriver).context(contextName);
                    System.out.println(threadLogger.deviceName + " Switching to: " + contextName);
                  // System.out.println("Switch To: " + contextName);
                }
        }

    }
}

Hope this helps someone!

I was able to fix my issue by using the Appium Record capability and use it for this code for example. In this case I am clicking on the phone Dialer native application. The Appium showed me this function

MobileElement el2 = (MobileElement) driver.findElement(By.xpath("//android.widget.RelativeLayout[@content-desc=“2”]/android.widget.TextView[1]"));
el2.click();
MobileElement el1 = (MobileElement)

Then I used it for this code to click on “2” button.

public static void main() throws MalformedURLException {

	//Set up desired capabilities and pass the Android app-activity and app-package to Appium
	DesiredCapabilities capabilities = new DesiredCapabilities();
	capabilities.setCapability("deviceName","G890A");
	capabilities.setCapability("BROWSER_NAME", "Android");
	capabilities.setCapability("VERSION", "7.0"); 
	capabilities.setCapability("platformName","Android");
	capabilities.setCapability("noReset", "True");
	capabilities.setCapability("appPackage", "com.samsung.android.contacts");
	capabilities.setCapability("appActivity", "com.android.dialer.DialtactsActivity");

	 WebDriver driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
	 MobileElement el2 = (MobileElement) driver.findElement(By.xpath("//android.widget.RelativeLayout[@content-desc=\"2\"]/android.widget.TextView[1]"));
		el2.click();

}

I have smae issue. I can find element, I click on it, no error message but nothing happened after I click on element.