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


#1

Helpppp meeeee! :((
I don’t unserstand why !!!
i’m findbyId but it’s error.
Here My code:
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;

public class TDTTCN {
// AppiumDriver driver;
AndroidDriver driver;
Dimension size;
WebDriverWait wait;
@BeforeTest
public void setUp() throws MalformedURLException {
// Created object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();

  // Set android deviceName desired capability. Set your device name.
  capabilities.setCapability("deviceName", "330030e3be3233f9");

  // Set android VERSION desired capability. Set your mobile device's OS version.
  capabilities.setCapability(CapabilityType.VERSION, "7.0");

  // Set android platformName desired capability. It's Android in our case here.
  capabilities.setCapability("platformName", "Android");

  // Set android appPackage desired capability. It is
  // com.android.calculator2 for calculator application.
  // Set your application's appPackage if you are using any other app.
  capabilities.setCapability("appPackage", "group3f.vn.vpet.quynhly");

  // Set android appActivity desired capability. It is
  // com.android.calculator2.Calculator for calculator application.
  // Set your application's appPackage if you are using any other app.
  capabilities.setCapability("appActivity", "group3f.longnt.vpet.Activities.LoginActivity");

  // Created object of RemoteWebDriver will all set capabilities.
  // Set appium server address and port number in URL string.
  // It will launch calculator app in android device.
  driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
 }
 
 @Test
 public void TDTTCN() throws ClassNotFoundException, SQLException {
	 
	 ConnDB condb = new ConnDB();
	 condb.conn = condb.getConnDB();
	 String query = "SELECT * FROM tdttcn";
	 Statement st = condb.conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
	 ResultSet rs = st.executeQuery(query);
	 int i = 1;
	 while (rs.next())
      {
		 Data_TDTTCN c = new Data_TDTTCN(rs);
		 System.out.print("\nAction " + i++ +"\n");
		 Action_TDTTCN(c);
		 driver.resetApp();
      }
 }

public void Action_TDTTCN(Data_TDTTCN data) throws ClassNotFoundException, SQLException {
driver.findElementById(“group3f.vn.vpet.quynhly:id/username”).sendKeys(“0384946866”);
driver.findElementById(“group3f.vn.vpet.quynhly:id/password”).sendKeys(“123456”);
driver.findElementById(“group3f.vn.vpet.quynhly:id/btn_login”).click();
//ko tim thay btn home

it’s error here:
driver.findElementById(“group3f.vn.vpet.quynhly:id/navigation_home”).click();

	 driver.findElementById("group3f.vn.vpet.quynhly:id/menu_more_item").click();
	 inputDataTDTTCN(data);
	driver.findElementById("group3f.vn.vpet.quynhly:id/btn_xac_nhan_sua").click();
	if(isElementWait("android:id/message", 5)) {
		 data.debug = "koTC";
	 }else {
		 data.debug = "TC";
	 }
	data.compare = compare(data.target, data.debug);	
	 
	 updateData(data);
}
 public boolean isFindClassName(String classname) {
	 try {
		driver.findElementByClassName(classname);
		 System.out.print("find: "+ classname + "\n ");
		return true;
	} catch (Exception e) {

		 System.out.print("null\n");
		return false;
	}
 }
 public boolean isElementWait(String name, int timeout){
     	try{

     		WebDriverWait wait = new WebDriverWait(driver, timeout);
			wait.until(ExpectedConditions.visibilityOf((MobileElement) driver.findElementById(name))); 
     	    return true;
     	}catch(Exception e){
     	    return false;
     	}
	 }
 public boolean compare(String target, String debug) {
	 if(target.trim().equals(debug.trim())) {
		 System.out.print("ket qua khop muc tieu. PASS!\n");
		 return true;
	 }
	 System.out.print("ket qua khong khop muc tieu. FAil!\n");
	 return false;
 }
 public void updateData(Data_TDTTCN data) throws ClassNotFoundException, SQLException {
	 ConnDB condb = new ConnDB();
	 condb.conn = condb.getConnDB();
	 String query = "UPDATE tdttcn SET debug='"+data.debug+"', compare="+data.compare+" WHERE id="+data.id;
	 PreparedStatement preparedStmt = condb.conn.prepareStatement(query);
	 preparedStmt.executeUpdate();
	 condb.closeConn();
	 System.out.print("update successfuly!\n");
 }
public void inputDataTDTTCN(Data_TDTTCN data) {
	driver.findElementById("group3f.vn.vpet.quynhly:id/edHoTen").sendKeys(data.username);
	driver.findElementById("group3f.vn.vpet.quynhly:id/edEmail").sendKeys(data.email);
}
public WebElement scroll(String parent, String child) {
	 String form_res = "new UiSelector().resourceIdMatches(\".*id/"+parent+"\")";
	 String form_elem = "new UiSelector().resourceIdMatches(\".*id/"+child+"\")";// 
	 WebElement elem = driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable("+form_res+")"+".scrollIntoView("+form_elem+")"));
	 return elem;
 }

@AfterTest
 public void End() {
  driver.quit();
 }

}


#2
  1. remove package name from “findElementById”. Left only “navigation_home”
  2. double check that only one such element exists. You can try “findElements!” instead and check it size.
  3. hide keyboard after type text

#3

Thanks for Your Reply!
i did Step 1, but
i don’t underdtand step 2,3 your answers. You can Clear it??


#4

@QuynhLy try:

        List<WebElement> list = driver.findElementsById("navigation_home");
        System.out.println("list size: " + list.size());

to check that only one such button exists on screen