NoSuchELementException when element is actually present

Hi, I meant the screenshot of your app with ‘Phone’ element highlighted, so that we can try to find a better locator for that element. You should try to access that element with a different locator options - id, class

@Venkatesh_Akula
Hi Venkatesh,

Please find the attached screenshot with the element highlighted in the red.

So you are trying to automate a third party application, ensure that you give the right package name and appActivity. Please check this link:

@Venkatesh_Akula

Hi,

Below is my code:

import java.net.MalformedURLException;

import java.net.SocketException;
import java.net.URL;
import java.util.Scanner;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

import com.convert.Ntow;

import io.appium.java_client.android.AndroidDriver;

public class Calling {
AndroidDriver dr;
@Test
public void call() throws MalformedURLException, WebDriverException, SocketException, InterruptedException
{

	DesiredCapabilities cp= new DesiredCapabilities();
	cp.setCapability("deviceName", "Moto C Plus");
	cp.setCapability("platformVersion", "7.0");
	cp.setCapability("platformName", "Android");
	cp.setCapability("appPackage", "com.motorola.launcher3");
	cp.setCapability("appActivity", "com.android.launcher3.GoogleNowPanel");
	
	dr = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"),cp);
	
	dr.findElementByAccessibilityId("Phone").click();
	dr.findElement(By.id("com.android.dialer:id/floating_action_button")).click();
	
	Ntow c = new Ntow();
	
	Scanner sc = new Scanner(System.in);
	System.out.print("Enter Mobile Number");
	
	int i; int arr[] = new int[10];
	int brr[]=new int[10];
	String[] srr= new String[10];

	
	for(i=0;i<10;i++)
	{
		arr[i]= sc.nextInt();
	}

for(int m=1;m<=5;m++)
{
for(int k:arr)
{
dr.findElement(By.id(“com.android.dialer:id/”+c.Convert(k)+"")).click();

	}
	dr.findElement(By.id("com.android.dialer:id/dialpad_floating_action_button")).click();
	
	Thread.sleep(12000);
	
	dr.findElement(By.id("com.android.dialer:id/floating_end_call_action_button")).click();
	
	}

}
}

Whenever I try to make the call 5 times after taking the mobile number from the user, the call is initiated for the first time but when it goes for the second iteration i got the below error:

I’m not sure what the above error is, I’m a Python guy. Sorry

Hi,
Keep in mind that implicit wait is not working anymore with uiAutomator2.
You are geting this error because the driver tries to find the element only 2 or 4 times in like 1 or 2 seconds and if is not finding anything will throw the error. Your element might still load. Try to put some Thread.sleep(2000) and see what happens.
I’m using explicit wait with uiAutomator2 and works like a charm:

public boolean waitForElementToBePresent(MobileElement element) {
try {
WebDriverWait wait = new WebDriverWait(driver, BaseSetup.EXPLICIT_WAIT_TIME, 500);
wait.until(ExpectedConditions.visibilityOf(element));
return true;
} catch (NoSuchElementException e) {
return false;
}
}

1 Like

@Zuzeac

Thank you, for your reply.

May I know Which class should I mentioned instead of BaseSetup in the below line:

WebDriverWait wait = new WebDriverWait(driver, BaseSetup.EXPLICIT_WAIT_TIME, 500);

I have tried the different way and I am getting error I have mentioned the code as well as attach the screenshot of the error:

import java.net.MalformedURLException;

import java.net.SocketException;
import java.net.URL;
import java.util.Scanner;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

import com.convert.Ntow;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;

public class Calling {

AndroidDriver dr;


@Test 
public void call() throws MalformedURLException, WebDriverException, SocketException, InterruptedException, NullPointerException
{
	WebDriverWait wait= new WebDriverWait(dr,20);
	DesiredCapabilities cp= new DesiredCapabilities();
	cp.setCapability("deviceName", "Moto C Plus");
	cp.setCapability("platformVersion", "7.0");
	cp.setCapability("platformName", "Android");
	cp.setCapability("appPackage", "com.motorola.launcher3");
	cp.setCapability("appActivity", "com.android.launcher3.GoogleNowPanel");
	
	dr = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"),cp);
	
	dr.findElementByAccessibilityId("Phone").click();
	dr.findElement(By.id("com.android.dialer:id/floating_action_button")).click();
	
	Ntow c = new Ntow();
	
	Scanner sc = new Scanner(System.in);
	System.out.print("Enter Mobile Number");
	
	int i; int arr[] = new int[10];
	int brr[]=new int[10];
	String[] srr= new String[10];

	
	for(i=0;i<10;i++)
	{
		arr[i]= sc.nextInt();
	}

for(int m=1;m<=5;m++)
{

	for(int k:arr)
	{
	WebElement dl= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("com.android.dialer:id/"+c.Convert(k)+"")));
	dl.click();
	}
	
	dr.findElement(By.id("com.android.dialer:id/dialpad_floating_action_button")).click();
	Thread.sleep(10000);
	dr.findElement(By.id("com.android.dialer:id/floating_end_call_action_button")).click();

}

}
	}

Error_calling_WebDriverWait

do it like this:
WebDriverWait wait= new WebDriverWait(dr,20,100);
BaseSetup is my class were I defined the EXPLICIT_WAIT time.

@Zuzeac

Thank you for your help now my code is working fine In case if I have any other issue I will get back to you.

Thank you once again.

@Sagar_Khanvilkar
I’m glad I could help you
Happy testing

Elements with xpath starts with //android.widget.Button is showing element not visible. Could someone help me.

@Venkatesh_Akula- I am getting error “no such element exception” in the code when I run it on mac but it works fine in windows. My app is hybrid and i change context to enter values in username field. Please help me to resolve this.

Code-:

		caps = new DesiredCapabilities();
		File apkPath = new File("test-executables");
		//.apk filepath
		File apkFile = new File(apkPath, "app-debug.apk");
		caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Pixel_XL");
		caps.setCapability(CapabilityType.PLATFORM_NAME, "ANDROID");
		caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "11.0");
		caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiAutomator2");
		caps.setCapability("autoGrantPermissions",false);
		caps.setCapability("autoAcceptAlerts", true);
		caps.setCapability(MobileCapabilityType.APP,apkFile.getAbsolutePath());
		caps.setCapability("unicodeKeyboard", true);
		caps.setCapability("resetKeyboard", true);
		caps.setCapability("gpsEnabled", false);
		caps.setCapability("newCommandTimeout", 3000);
		mobileDriver.set(new AndroidDriver<MobileElement>(new URL(localHub), caps));
		//webDriver.set(new RemoteWebDriver(new URL(localHub), caps));
		Thread.sleep(30000);

public void login() throws InterruptedException {

driver .context(“WEBVIEW_com.xxx.xxx.xx.xx”);

Thread. sleep (5000);

driver .findElement(By. xpath ("//*[@id=‘loginform-username’]")).sendKeys(“xxxx”);

driver .findElement(By. xpath ("//*[@id=‘loginform-password’]")).sendKeys(“xxx”);

driver .findElement(By. xpath ("//a[@id=‘login-click’]")).click();

}

Form

Error-

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:“xpath”,“selector”:"//*[@id=‘loginform-username’]"}

(Session info: chrome=91.0.4472.114)

For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html

Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:17:03’

System info: host: ‘AMAV1Y639WCK0’, ip: ‘2401:4900:1f3d:e143:9fd:8762:33ff:7692%en0’, os.name: ‘Mac OS X’, os.arch: ‘x86_64’, os.version: ‘10.16’, java.version: ‘15.0.2’

Driver info: io.appium.java_client.android.AndroidDriver

Capabilities {app: /Users/shweta.k.gupta/Deskt…, appPackage: com.accenture.de.aok.ga, autoAcceptAlerts: true, autoGrantPermissions: false, automationName: uiAutomator2, databaseEnabled: false, desired: {app: /Users/shweta.k.gupta/Deskt…, autoAcceptAlerts: true, autoGrantPermissions: false, automationName: uiAutomator2, deviceName: Pixel_XL, gpsEnabled: false, newCommandTimeout: 3000, platformName: android, platformVersion: 11.0, resetKeyboard: true, unicodeKeyboard: true}, deviceApiLevel: 30, deviceManufacturer: Google, deviceModel: sdk_gphone_arm64, deviceName: emulator-5554, deviceScreenDensity: 560, deviceScreenSize: 1440x2560, deviceUDID: emulator-5554, gpsEnabled: false, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: true, newCommandTimeout: 3000, pixelRatio: 3.5, platform: LINUX, platformName: ANDROID, platformVersion: 11, resetKeyboard: true, statBarHeight: 84, takesScreenshot: true, unicodeKeyboard: true, viewportRect: {height: 2308, left: 0, top: 84, width: 1440}, warnings: {}, webStorageEnabled: false}

Session ID: 5528cbad-e274-4643-b122-ab05155ac1a4

*** Element info: {Using=xpath, value=//*[@id=‘loginform-username’]}

at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)

at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)

at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)

at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)

at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)

at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)

at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)

at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:250)

at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)

at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:41)

at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)

at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)

at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)

at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:61)

at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)

at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)

at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)

at io.appium.java_client.DefaultGenericMobileDriver.findElementByXPath(DefaultGenericMobileDriver.java:151)

at io.appium.java_client.AppiumDriver.findElementByXPath(AppiumDriver.java:1)

at io.appium.java_client.android.AndroidDriver.findElementByXPath(AndroidDriver.java:1)

at com.appium.pagesMethodMobile.LoginPageMethods.login(LoginPageMethods.java:80)

at com.appium.testCaseMobile.LoggedInUserTest.LoggedInHomeScreenTest(LoggedInUserTest.java:44)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.base/java.lang.reflect.Method.invoke(Method.java:564)

at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)

at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)

at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)

at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)

at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)

at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)

at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)

at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)

at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

at org.testng.TestRunner.privateRun(TestRunner.java:794)

at org.testng.TestRunner.run(TestRunner.java:596)

at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)

at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)

at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)

at org.testng.SuiteRunner.run(SuiteRunner.java:276)

at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)

at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)

at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)

at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)

at org.testng.TestNG.runSuites(TestNG.java:1063)

at org.testng.TestNG.run(TestNG.java:1031)

at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)

at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)

at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

Line Number==>>144 : Inside Class : com.test.fw.testng.TestBaseStandalone :Enter Method : captureFinalScreenshot

Line Number==>>159 : Inside Class : com.test.fw.testng.TestBaseStandalone :Exit Method : captureFinalScreenshot

log4j:WARN No appenders could be found for logger (freemarker.cache).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

login form code-

input type=“text” id=“loginform-username” class=“form-control form-control–stable” name=“LoginForm[username]” maxlength=“12” placeholder=“Benutzername *” autocomplete=“new-password” tabindex=“1” style=“font-size: 14px” data-field-required=“true” data-field-required-msg=“Benutzername darf nicht leer sein.” data-ogs-auto-def=“loginform_username_1”