A web element is seemingly clicked but in fact no action is performed (Android)

Hi, could you please help me to sort the following problem out? I have been struggling with this problem for a long time. When I navigate to a web page and click on a web element then no errors occur, it looks like it was successful but on physical Android mobile I cannot see the click action was actually performed (even if I debug the test)
I would really appreciate if someone could try the same scenario on his Android device/simulator as I am really hopeless. I am attaching my code. The web page I use is public.Thank you in advance for any help or suggestions.

namespace Selenium_AndroidDriver
{
[TestFixture()]
public class Class7
{
public IWebDriver driver;
private string baseUrl;
private IWebElement LoginIcon;

    [TestFixtureSetUp]
    public void BeforeAll()
    {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.SetCapability("automationName", "Appium");
        capabilities.SetCapability("browserName", "Chrome");
        capabilities.SetCapability("platformName", "Android");
        capabilities.SetCapability("deviceName", "Samsung");
        capabilities.SetCapability("platformVersion", "4.4.2");
        capabilities.SetCapability(CapabilityType.BrowserName,"chrome");
        //capabilities.SetCapability(CapabilityType.AcceptSslCertificates,true);
        //capabilities.SetCapability(CapabilityType.Proxy, true);
        baseUrl = "https://mobile.betway.com";
        driver = new RemoteWebDriver(new Uri("http://localhost:4275/wd/hub"), capabilities);
    }

    [Test()]
    public void AndroidByAppium2()
    {
        driver.Url = baseUrl + "/";
        driver.FindElement(By.Id("LoginIcon")).Click();
    }
}

}

The problem I encountering is with the clicking on the LoginIcon.

4 Likes

Alright, this is interesting one :smile:
I spent some time on investigation, tried ChromeDriver and Selendroid, tried clicking by coordinates… Didn’t work. Finally, I switched to native context and found out, that HTML code is “transferred” into native code, custom WebView, I suppose.
Basically, what you need to do, is:

1. Run AppiumDriver with ChromeDriver (or simply pass autoWebview capability with false)
2. Switch to native context
3. Find element and click on it. Example:

driver.FindElement(By.Name("Login")).Click();

To find right selectors, you can use uiautomatorviewer (doesn’t work if Appium server is running), or page source function:

driver.getPageSource();

If you will have problems with getting other elements, let me know!

4 Likes

Hi Kirill, thank you very much for your time, it is much appreciated. I have been struggling with this problem for a long time. Regarding Selendroid, I was able to make it work through Selendroid but I was looking for a solution which I could use for both Android and IOS devices and Appium seems to be the way.
I am going to try your suggested solution today. I have got just a question related to the 2nd step as I am not entirely sure what do you mean by that?
Could you please provide me with your source code?
Thanks mate!

Neither AppiumDriver, neither Selendroid doesn’t work for me within web context. Both work within native context though. I would suggest to stick to AppiumDriver driver and work with elements within native context. Simply switch to native context on launching browser. For me, autoWebview doesn’t work properly… I opened an issue on that.
Your capabilities would be the same. Your code would be next:

driver.Url = baseUrl + "/";
driver.context("NATIVE_APP");
driver.FindElement(By.Name("Login")).Click();

However on iOS I was not able to find elements by name in some reason… Basically, that’s how I did it:

driver.Url = baseUrl + "/";
driver.context("NATIVE_APP");
WebElement loginButton = driver.FindElement(By.Xpath("//UIAStaticText[@name="Login"]"));

And I clicked on the element using Actions class. I didn’t work with this class on Java, but here is Ruby code:

$app_driver.action.move_to(loginButton, 0, 0).click.perform

Hey Kirill, I tried that but when i want to set the context to the “NATIVE_CONTEXT” then I get the “No such context found” error. Is the string “NATIVE_CONTEXT” a constant or should it be replaced by a specific value for corresponding page. I am attaching my code (using c# binding). Thank you for your help.

[TestFixture]
public class Class1
{
public string baseUrl;
public AppiumDriver driver;

    [TestFixtureSetUp]
    public void BeforeAll()
    {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.SetCapability("automationName", "Appium");
        capabilities.SetCapability("platformName", "Android");
        capabilities.SetCapability("deviceName", "Samsung");
        capabilities.SetCapability("platformVersion", "4.2.2");
        capabilities.SetCapability(CapabilityType.BrowserName, "chrome");
       

        baseUrl = "https://mobile.betway.com";
        driver = new AndroidDriver(new Uri("http://localhost:4723/wd/hub"), capabilities);
    }

    [Test]
    public void TestBetway()
    {
        driver.Url = baseUrl + "/";
        driver.Context = "NATIVE_CONTEXT";
        driver.FindElement(By.Name("Login")).Click();
        
    }
    [TestFixtureTearDown]
    public void Closedriver()
    {
        driver.Quit();
    }
}
1 Like

My bad. I meant NATIVE_APP, not NATIVE_CONTEXT. Use available contexts method to check what contexts are available, by the way.

Thank you for your help Kirill. I have been able to click on the element! Need to get more information about context as it is still a bit cloudy for me though. When I open the page on a mobile device and then run google inspector to see web page elements then google crashes. So I do not know how to analyze the page yet but clicking on the element is a big step for me anyway. :smile:
If you have any useful links or information related to this problematic then they would be much appreciated.

Don’t use debugger while running Appium server. Either use driver.getPageSource(); method to get source code, either use debugger, but shut down Appium server before.

Hi ,
Unfortunately, the context switch doesn’t work for me , I’m switching the context at the beginning of the test (from NATIVE to WEBVIEW) , and I do able to recognize / find the LogIn button , but once I click on it , nothing happens , but I do get 200 ‘OK’ response in Appium logs …

Any idea how it can be solved ?

@igal_epshtein, you should use TouchActions class to tap on element.

@kirill tried with the TouchActions as well , but didn’t work.
getting the same ‘200’ response , but nothing really happens

1 Like

Alright, could you try TouchActions one more time but using center coordinates of the element?
Let’s say element is your WebElement instance. To get center of the element you can use getLocation() and getSize() methods. Like that:

Point location = element.getLocation();
Dimension size = element.getSize();
double x = element.location.getX() + size.getWidth()/2.0;
double y = element.location.getY() + size.getHeight()/2.0;

Now try to click by coordinates x and y.

Also you should try JS (should be faster 2-3 times than TouchActions):

WebElement element = driver.findElement('...');
JavascriptExecutor js = (JavascriptExecutor) driver;
Point location = element.getLocation();
Dimension size = element.getSize();
double x = element.location.getX() + size.getWidth()/2.0;
double y = element.location.getY() + size.getHeight()/2.0
HashMap<String, Double> point = new HashMap<String, Double>();
point.put("x", x);
point.put("y", y;
js.executeScript("mobile: tap", point);

Hi,
Tired that as well , nothing really happens (I even don’t get logs for the tap operation)

 Marker - Apr 28, 2015, 10:24:30 AM
2015-04-28 07:24:32:017 - info: --> POST /wd/hub/session/8cb1b65d-9b33-41a8-9525-7fe0d237ee80/element {"using":"id","value":"cvm_btn_login_button"}
2015-04-28 07:24:32:018 - info: [debug] Proxying command to 127.0.0.1:9515
2015-04-28 07:24:32:018 - info: [debug] Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/session/d1db0b0086e2b8cbfd4ea17dc03f5be8/element","method":"POST","json":{"using":"id","value":"cvm_btn_login_button"}}
2015-04-28 07:24:32:048 - info: [debug] Proxied response received with status 200: {"sessionId":"d1db0b0086e2b8cbfd4ea17dc03f5be8","status":0,"value":{"ELEMENT":"0.864398856414482-3"}}
2015-04-28 07:24:32:049 - info: <-- POST /wd/hub/session/8cb1b65d-9b33-41a8-9525-7fe0d237ee80/element 200 31.998 ms - 101 
2015-04-28 07:24:35:924 - info: --> GET /wd/hub/session/8cb1b65d-9b33-41a8-9525-7fe0d237ee80/element/0.864398856414482-3/location {}
2015-04-28 07:24:35:925 - info: [debug] Proxying command to 127.0.0.1:9515
2015-04-28 07:24:35:926 - info: [debug] Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/session/d1db0b0086e2b8cbfd4ea17dc03f5be8/element/0.864398856414482-3/location","method":"GET"}
2015-04-28 07:24:35:949 - info: [debug] Proxied response received with status 200: "{\"sessionId\":\"d1db0b0086e2b8cbfd4ea17dc03f5be8\",\"status\":0,\"value\":{\"ceil\":{},\"clone\":{},\"floor\":{},\"round\":{},\"scale\":{},\"toString\":{},\"translate\":{},\"x\":38.40625,\"y\":312.5}}"
2015-04-28 07:24:35:950 - info: <-- GET /wd/hub/session/8cb1b65d-9b33-41a8-9525-7fe0d237ee80/element/0.864398856414482-3/location 200 26.557 ms - 175 
2015-04-28 07:24:38:709 - info: --> GET /wd/hub/session/8cb1b65d-9b33-41a8-9525-7fe0d237ee80/element/0.864398856414482-3/size {}
2015-04-28 07:24:38:709 - info: [debug] Proxying command to 127.0.0.1:9515
2015-04-28 07:24:38:710 - info: [debug] Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/session/d1db0b0086e2b8cbfd4ea17dc03f5be8/element/0.864398856414482-3/size","method":"GET"}
2015-04-28 07:24:38:722 - info: [debug] Proxied response received with status 200: "{\"sessionId\":\"d1db0b0086e2b8cbfd4ea17dc03f5be8\",\"status\":0,\"value\":{\"ceil\":{},\"clone\":{},\"floor\":{},\"height\":48,\"round\":{},\"scale\":{},\"toString\":{},\"width\":307}}"
2015-04-28 07:24:38:723 - info: <-- GET /wd/hub/session/8cb1b65d-9b33-41a8-9525-7fe0d237ee80/element/0.864398856414482-3/size 200 14.425 ms - 161 
2015-04-28 07:25:18:391 - info: --> GET /wd/hub/status {}
2015-04-28 07:25:18:392 - info: [debug] Proxying command to 127.0.0.1:9515
2015-04-28 07:25:18:393 - info: [debug] Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/status","method":"GET"}
2015-04-28 07:25:18:396 - info: [debug] Proxied response received with status 200: "{\"sessionId\":\"\",\"status\":0,\"value\":{\"build\":{\"version\":\"alpha\"},\"os\":{\"arch\":\"x86_64\",\"name\":\"Mac OS X\",\"version\":\"10.10.3\"}}}"
2015-04-28 07:25:18:397 - info: <-- GET /wd/hub/status 200 5.917 ms - 126

just to make it clear , I’m using an AndroidDriver wich was switched with context to the “WEBVIEW” - is it ok ?

Yeah.
Could you please provide the code where you tap on the element?

WebElement element = driver.findElement(By.id("cvm_btn_login_button")); //should be switched to name instead of text
            JavascriptExecutor js = (JavascriptExecutor) driver;
            Point p = element.getLocation();
            Dimension size = element.getSize();
            double x = p.getX() + size.getWidth() / 2.0,
                       y = p.getY() + size.getHeight() / 2.0;
            HashMap<String , Double> point = new HashMap<String , Double>();
            point.put("x" , x);
            point.put("y" , y);
            js.executeScript("mobile: tap", point);

it’s your code :smile:
Actually , no matter What I’m trying , nothing works.

The one thing I’ve found which is working is some YUI js code which is being injected into the webpage , but I’ve some issues with proxy (the code should be downloaded from YUI repository)

@kirill - any idea ?

10x

What does Appium server return on tap?

tap with jsexecutor :

info: --> POST /wd/hub/session/855425a3-10ec-4029-9b69-9b6d8eab8169/element {"using":"id","value":"cvm_login_button"}
info: [debug] Proxying command to 127.0.0.1:9515
info: [debug] Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/session/5ca490e8529b8c3282b1189d1dfe656d/element","method":"POST","json":{"using":"id","value":"cvm_login_button"}}
info: [debug] Proxied response received with status 200: {"sessionId":"5ca490e8529b8c3282b1189d1dfe656d","status":0,"value":{"ELEMENT":"0.07824705308303237-4"}}
info: <-- POST /wd/hub/session/855425a3-10ec-4029-9b69-9b6d8eab8169/element 200 51.607 ms - 103 
info: --> GET /wd/hub/session/855425a3-10ec-4029-9b69-9b6d8eab8169/element/0.07824705308303237-4/location {}
info: [debug] Proxying command to 127.0.0.1:9515
info: [debug] Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/session/5ca490e8529b8c3282b1189d1dfe656d/element/0.07824705308303237-4/location","method":"GET"}
info: [debug] Proxied response received with status 200: "{\"sessionId\":\"5ca490e8529b8c3282b1189d1dfe656d\",\"status\":0,\"value\":{\"ceil\":{},\"clone\":{},\"floor\":{},\"round\":{},\"scale\":{},\"toString\":{},\"translate\":{},\"x\":38.40625,\"y\":312.5}}"
info: <-- GET /wd/hub/session/855425a3-10ec-4029-9b69-9b6d8eab8169/element/0.07824705308303237-4/location 200 17.603 ms - 175 
info: --> GET /wd/hub/session/855425a3-10ec-4029-9b69-9b6d8eab8169/element/0.07824705308303237-4/size {}
info: [debug] Proxying command to 127.0.0.1:9515
info: [debug] Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/session/5ca490e8529b8c3282b1189d1dfe656d/element/0.07824705308303237-4/size","method":"GET"}
info: [debug] Proxied response received with status 200: "{\"sessionId\":\"5ca490e8529b8c3282b1189d1dfe656d\",\"status\":0,\"value\":{\"ceil\":{},\"clone\":{},\"floor\":{},\"height\":48,\"round\":{},\"scale\":{},\"toString\":{},\"width\":307}}"
info: <-- GET /wd/hub/session/855425a3-10ec-4029-9b69-9b6d8eab8169/element/0.07824705308303237-4/size 200 14.737 ms - 161 
info: --> POST /wd/hub/session/855425a3-10ec-4029-9b69-9b6d8eab8169/execute {"script":"mobile: tap","args":[{"y":336,"x":191}]}
info: [debug] Pushing command to appium work queue: ["click",{"x":191,"y":336}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"click","params":{"x":191,"y":336}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: click
info: [debug] [BOOTSTRAP] [debug] Display bounds: [0,0][768,1184]
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":true,"status":0}
info: [debug] Responding to client with success: {"status":0,"value":true,"sessionId":"855425a3-10ec-4029-9b69-9b6d8eab8169"}
info: <-- POST /wd/hub/session/855425a3-10ec-4029-9b69-9b6d8eab8169/execute 200 121.564 ms - 76 {"status":0,"value":true,"sessionId":"855425a3-10ec-4029-9b69-9b6d8eab8169"}

Last line " execute " … but nothing really happens with the button

You know what would be funny? If your button was truly broken, hence why nothing is happening. I only mention this as I had tunnel vision recently during some new test development. I couldn’t figure out why my test code was returning a failure when I expected a pass. I thought my test scripts were bad, or Appium had some problem. It took me a while before I finally realized the app had a bug and was not working as expected, my tests were correct, and the reported failure was accurate. :slight_smile:

@Christopher_Graham the thing is , the button works perfectly while tapping on it (real tapping) also I did managed to click / tap on the button by uisng some YUI js framework , the problem is I have some proxy issues and I cannot download the YUI’s code from the network , that’s why I’ve decided to perform a click / tap by using a traditional appium / selenium’s methods …