“An element could not be located on the page using the given search parameters.”


#1

I have Appium Test script recorded by Appium
Here is my script

package com.hostname.myapp;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;


public class AppiumTest {

private static AppiumDriver<AndroidElement> wd;

@BeforeClass
public static void setUpClass() throws MalformedURLException {

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("appium-version", "1.0");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("platformVersion", "5.0.2");
    capabilities.setCapability("deviceName", "XXXXXXXXXXXX");
    capabilities.setCapability("app", "/Users/X/X.apk");
    capabilities.setCapability("appPackage", "com.hostname.myapp");
    wd = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}

@AfterClass
public static void tearDownClass() {
    if (wd != null) {
        wd.quit();
    }
}

@Test
public void test() throws IOException {
    wd.findElement(By.xpath("//android.widget.LinearLayout[1]/.../android.widget.EditText[1]")).click();
    wd.findElement(By.xpath("//android.widget.LinearLayout[1]/.../android.widget.EditText[1]")).sendKeys("username");
    wd.findElement(By.xpath("//android.widget.LinearLayout[1]/.../android.widget.EditText[2]")).click();
    wd.findElement(By.name("//android.widget.LinearLayout[1]/.../android.widget.EditText[2]")).sendKeys("password");
    wd.findElement(By.xpath("//android.widget.LinearLayout[1]/.../android.widget.Button[1]")).click();
    wd.close();
} 

After I package into MAVEN project and uploaded to AWS Device Farm.
The script run failed, and the error message is:

An element could not be located on the page using the given search parameters.
(WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 10.27 seconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: ‘2.50.1’,
revision: ‘d7fc91b29de65b790abb01f3ac5f7ea2191c88a7’,
time: ‘2016-01-29 11:11:26’
System info: host: ‘ip-172-31-13-65’,
ip: ‘172.31.13.65’,
os.name: ‘Linux’,
os.arch: ‘amd64’,
os.version: ‘3.13.0-53-generic’,
java.version: ‘1.8.0_65’
Driver info: io.appium.java_client.android.AndroidDriver Capabilities [{networkConnectionEnabled=true, desired={}, platformVersion=6.0.1, warnings={}, webStorageEnabled=false, locationContextEnabled=false, browserName=Android, takesScreenshot=true, javascriptEnabled=true, databaseEnabled=false, deviceName=6e4c6027, platform=LINUX}] Session ID: c0ec736d-c4d8-4b3a-8d3c-1beb24dd0782 ***
Element info: {Using=xpath, value=//android.widget.LinearLayout[1]/…/android.widget.EditText[1]}

If I am not wrong, my parameters of my elements are something wrong
But these path/parameter are recorded by Appium Insector
They should be correct, aren’t them?

I did Google search and I found an answer which told me to change xpath into

By.xpath("//android.widget.EditText[@text=‘Email ID’]")

But what should I do when I having 2 Textbox? I have username and password textbox

Thank a lot.


#2

This means your search parameter is not correct, use By.xpath("//android.widget.EditText[@text=‘Email ID’]"), you have two edittext, but only password edittext fits the xpath, about another one, change the text value, or use other properties.


#3

Hi Lee, thanks again for answering my questions
But I still didn’t get it
My search parameter is recorded by Appium Inspector,
it’s work when I clicked “Replay” in Appium Inspector. Why it won’t be correct?

I have 2 textbox:
//android.widget.LinearLayout/.../android.widget.EditText[1] is username textbox
//android.widget.LinearLayout/.../android.widget.EditText[2] is password textbox
How should I write into your form style?

Thanks you


#4

I think you copied them directly from inspector. Not correct without inspector context.
try //android.widget.EditText[1] and //android.widget.EditText[2]
btw, many ways to find the elements, plz refer to the xpath grammar.


#5

Yes, I copied from Inspector.
Do you mean I am doing wrong if I copied them?
What is the correct way to do so?

I did try //android.widget.EditText[1] and //android.widget.EditText[2]
My code is about:

wd.findElement(By.xpath("//android.widget.EditText[1]")).sendKeys("email");
wd.findElement(By.xpath("//android.widget.EditText[2]")).sendKeys("password");

And the error message is

Not yet implemented. Please help us: http://appium.io/get-involved.html (WARNING: The server did not provide any stacktrace information)


#6

Can you show me some example to edit to make the script from Inspector workable? Thanks you


#7

I don’t record the test script via appium, write python script directly.
about java, refer to appium examples:
https://github.com/appium/sample-code/blob/master/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/AndroidContactsTest.java


#8

Thank Lee, now I almost understand.
I will try to do so.
If we can’t use the script recorded by Appium Inspector, is it useless?
What is its main function?


#9

Use ----> WebDriver driver; instead of AppiumDrivrer or AndroidDriver.

Use -----> RemoteWebDriver instead of AndroidDriver ie… new RemoteWebDriver(new URL(“url here”));

then use…

findElement(by.xpath(“xpath of element”);

then there will be no error…

bye.


#10

Hi @kalvinlow i’m facing same issue.If your issue is resolved means can you help me


#11

I have tried your suggestion… It did not make any difference. Still getting same error…