Not able to locate element on mobile websites

Hi,

Currently I’m trying to get hands-on for mobile websites through appium. Following is the steps I try to automate.

  • Open a website, say quora.com
  • Try to click on Login button and enter credentials.

First steps launches the website in chrome, that’s working fine, but when it comes to the second steps, to click on login button, it fails.

Is there anything else which needs to be set-up for the website?

Thanks in advance.

Here is the piece of code which I used.

package com.MobileAppTest.AppiumTest;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
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.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;






	public class AndroidWebTest {

		
		public AndroidDriver<WebElement> driver, driver1;
		DesiredCapabilities capabilities, capabilities1;
		DefaultExecuteResultHandler resultHandler, resultHandler1;		
		DefaultExecutor executor1, executor ;
		public String Screenshotpath = "D:\\star\\";
		WebDriverWait wait;
		WebElement element;
		Logger log = Logger.getLogger("devpinoyLogger");

	    @BeforeClass
	    public void setUp() throws Exception {
	    	
	    
	    	capabilities = new DesiredCapabilities();
	        capabilities.setCapability("deviceName", "192.168.1.3:5555");
	        capabilities.setCapability("platformVersion", "5.0.1");
	             capabilities.setCapability("newCommandTimeout", 60000);
	      
	        capabilities.setCapability("fullreset", false);
	        capabilities.setCapability("platformName", "android");
	        capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "chrome");
	       
	        try
	        {
	        	CommandLine command = new CommandLine("cmd");
	        	command.addArgument("/c");
	        	command.addArgument("E:\\Appium\\Appium\\node.exe");
	        	command.addArgument("E:\\Appium\\Appium\\node_modules\\appium\\bin\\appium.js");
	        	command.addArgument("--address");
	        	command.addArgument("127.0.0.1");
	        	command.addArgument("--port");
	        	command.addArgument("4723");
	        	command.addArgument("--log");
	        	command.addArgument("C:\\Users\\MY\\Desktop\\snap.txt");
	        	
	        	resultHandler = new DefaultExecuteResultHandler();
	        	executor = new DefaultExecutor();
	        	executor.setExitValue(1);
	        	System.out.println("command: "+command);
	        	executor.execute(command, resultHandler);

	        	Thread.sleep(15000);

	        	driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
	        }
	        catch(Exception e)
	        {
	        	System.out.println(e.getCause());
	        	System.out.println(e.getMessage());
	        	log.info("This is an Info message!");
		        log.debug("This is a debug message");
		        log.error("This is an error message");
		        log.fatal("This is a failure message");
	        }
	       
	    }

	    @Test (priority = 1)
	    public void termsAndConditions()
	    {
	    	
	    		
		       
	    		driver.get("https://www.quora.com");
	    		String ContextVaule = driver.getContext();
	    		driver.context(ContextVaule);
	    		driver.findElement(By.xpath("//a[@id='__w2_L2y9RFO_signup_component']/div[2]/a[1]")).click();
	    		
	    		
	    		System.out.println(driver.getTitle());
		   
	    }
	    
	
	   @AfterClass
	   public void tearIT() throws ExecuteException, IOException
	   {
		   CommandLine command = new CommandLine("cmd");
		   command.addArgument("/c");
		   command.addArgument("taskkill");
		   command.addArgument("/F");
		   command.addArgument("/IM");
		   command.addArgument("node.exe");
		   resultHandler1 = new DefaultExecuteResultHandler();
   			executor1 = new DefaultExecutor();
   			executor1.setExitValue(1);
   			System.out.println("command: "+command);
   			executor1.execute(command, resultHandler1);
   			driver.quit();
	   }
	   
	   public void takescreenshot(String screenshot) throws IOException
	   {
		   File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
			FileUtils.copyFile(scrFile, new File(Screenshotpath+screenshot+".png"));
	   }
	
}

Are you working on the NATIVE contexts or the WEBVIEW contexts? I would highly recommend you to change to WEBVIEW contexts, and then start getting the elements by class or id.