Click() doesn't work and if use tap() instead it throw exception: The coordinates provided to an interactions operation are invalid, Coordinate [..] is outside of element rect: [..][..], status:29

Hi all,

My code are below:AppiumLog.txt (39.2 KB)

AppiumServerOnWindows appium = new AppiumServerOnWindows();
appium.startAppium3();
Thread.sleep(5000);
appium.setUp();
Thread.sleep(10000);

WebElement we;
we = appium.driver.findElement(By.xpath("//android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.webkit.WebView[1]/android.view.View[1]/android.widget.Button"));

//we.click(); // -> it hasn’t any exception but it doesn’t work so i use tap() function instead
appium.driver.tap(1, we.getLocation().x + (we.getSize().width/2), we.getLocation().y + (we.getSize().height/2), 300);


*** I’m testing on Native app on Android Emulator (Android 4.4.2 - API 19) on Windows 7 with Appium for Window (1.4.0 latest version for windows).

*** My code is try to click on a button. The .click() function hasn’t any exception but it doesn’t work and i try to use .tap() instead but it throws exception below:

[debug] Pushing command to appium work queue: [“element:touchDown”,{“x”:730,“y”:712}]
[debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:“action”,“action”:“element:touchDown”,“params”:{“x”:730,“y”:712}}
[debug] [BOOTSTRAP] [debug] Got command of type ACTION
[debug] [BOOTSTRAP] [debug] Got command action: touchDown
[debug] Responding to client with error: {“status”:29,“value”:{“message”:“The coordinates provided to an interactions operation are invalid.”,“origValue”:“Coordinate [x=730.0, y=712.0] is outside of element rect: [0,0][490,796]”},“sessionId”:“a7324862-0997-446c-a9b9-b338f436cb4e”}
e[37m<-- POST /wd/hub/session/a7324862-0997-446c-a9b9-b338f436cb4e/touch/perform e[39me[31m500e[39me[90m 92.080 ms - 240e[39m e[90me[39m
[debug] [BOOTSTRAP] [debug] Display bounds: [0,0][490,796]
[debug] [BOOTSTRAP] [debug] Returning result: {“value”:“Coordinate [x=730.0, y=712.0] is outside of element rect: [0,0][490,796]”,“status”:29}
Exception:
org.openqa.selenium.interactions.InvalidCoordinatesException: The coordinates provided to an interactions operation are invalid

(the Appium Log file is uploaded with this topic)

Please help me, thanks in advance

This class “android.webkit.WebView” means your app contains WebView. You need to first switch to webview contect and then perform click or tap.

@Test
public void AppLogin() throws InterruptedException{

	    System.out.println("AppLogin() :: driver.start() executed");
	    By webView = By.className("android.webkit.WebView");
	    By title = By.id("android:id/title");
	    WebDriverWait wait = new WebDriverWait(driver,300);
	    driver.findElement(title).getText();
	    Set<String> availableContexts1 = driver.getContextHandles();

System.out.println("Total No of Context Found Before reaching WebView = "+ availableContexts1.size());
System.out.println("Context Name is "+ availableContexts1);

	//4.1 Navigate to a portion of your app where a web view is active
		driver.findElement(By.id("com.mkyong.android:id/buttonUrl")).click();

wait.until(ExpectedConditions.visibilityOfElementLocated(webView));

// 4.2 Call getContext() method which will returns a list of contexts we can access, like ‘NATIVE_APP’ or ‘WEBVIEW_1’
Set availableContexts = driver.getContextHandles();
System.out.println("Total No of Context Found After we reach to WebView = "+ availableContexts.size());
for(String context : availableContexts) {
if(context.contains(“WEBVIEW”)){
System.out.println("Context Name is " + context);
// 4.3 Call context() method with the id of the context you want to access and change it to WEBVIEW_1
//(This puts Appium session into a mode where all commands are interpreted as being intended for automating the web view)
driver.context(context);
break;
}
}

String input_box_text = driver.findElement(By.id(“name_input”)).getAttribute(“value”);
System.out.println("Pre written text inside text box is " + input_box_text);

	driver.findElement(By.id("name_input")).clear();

driver.findElement(By.id(“name_input”)).sendKeys(“Amit Jain”); System.out.println(“No of dropdown on page “+ driver.findElements(By.xpath(”//select”)).size());
int size=driver.findElements(By.xpath("//select")).get(0).findElements(By.xpath("//option")).size();
System.out.println("No of Elements in dropdown "+ size);

	   WebElement car = driver.findElement(By.name("car"));
	   Select preferedCar=new Select(car);
	   preferedCar.selectByIndex(2);
	   		
	   
	   System.out.println("Button Value is : " + driver.findElement(By.xpath("/html/body/form/div/input[2]")).getAttribute("value"));
	   
	   //Key code constant: Back key.
	   //Constant Value: 4 (0x00000004)
	   driver.sendKeyEvent(4);

// 4.4 To stop automating in the web view context we can simply call the context again with id NATIVE_APP.

for(String context : availableContexts) {
if(context.contains(“NATIVE”)){
System.out.println("We are Back to " + context);
driver.context(context);
if (driver.findElement(title).getText().equals(“WebViewApp”))
System.out.println(“Context Switched”);
}

	   }
	   
	}
1 Like

Dear amitjaincoer191,

I changed my code as your suggest as below:
.

AppiumServerOnWindows appium = new AppiumServerOnWindows();
appium.startAppium3();
Thread.sleep(5000);
appium.setUp();
Thread.sleep(10000);

AndroidDriver driver = appium.driver;
WebElement we;

//--------- amitjaincoer191 code ---------------
WebDriverWait wait = new WebDriverWait(driver,300);

Set availableContexts1 = driver.getContextHandles();
System.out.println("Total No of Context Found Before reaching WebView = "+ availableContexts1.size());
System.out.println("Context Name is: "+ availableContexts1);

wait.until(ExpectedConditions.visibilityOfElementLocated(webView));

// 4.2 Call getContext() method which will returns a list of contexts we can access
Set availableContexts = driver.getContextHandles();
System.out.println("Total No of Context Found After we reach to WebView = "+ availableContexts.size());

for(String context : availableContexts) {
if(context.contains(“WEBVIEW”)){
System.out.println("Select Context Name: " + context);
driver.context(context);
break;
}
}
//----------------------------------------

// There are 2 available contexts: NATIVE_APP, WEBVIEW_com.ionicframework…
// After change context to WEBVIEW_…, run .findElement(…) and .click function()

we = driver.findElement(By.xpath("//android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.webkit.WebView[1]/android.view.View[1]/android.widget.Button"));
we.click();

//----------------------------------------

*** But it still throw exception “no such element” at the line:
we = driver.findElement(By.xpath("//android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.webkit.WebView[1]/android.view.View[1]/android.widget.Button"));

Exception:

Proxying [POST /wd/hub/session/a58a5549-5073-4d1d-aeca-c69a525263ca/element] to [POST http://127.0.0.1:9515/wd/hub/session/81f5dbc5a34601e87972799302d0e735/element] with body: {“using”:“xpath”,“value”:"//android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.webkit.WebView[1]/android.view.View[1]/android.widget.Button"}

Got response with status 200: {“sessionId”:“81f5dbc5a34601e87972799302d0e735”,“status”:7,“value”:{“message”:"no such element\n (Session info: webview=)\n (Driver info: chromedriver=2.15.322448 (52179c1b310fec1797c81ea9a203268398…
Exception:

org.openqa.selenium.NoSuchElementException: no such element
(Session info: webview=)

Please see my log here.AppiumLog.txt (36.4 KB)

No Such Element is correct because xpath is wrong . In webview context you cannot use native element xpath

Inspect your app in chrome remote debugger then you will get exact path of element inside webview. Consider it’s a plain selenium element like web application

1 Like

Dear amitjaincoer191,

As your suggest, now i’m using Chrome Developer Tools connect to my Android Emulator to investigate these element in webview context like Selenium with web objects.

Thank you so much for your great help, this is really much appreciated.