0
WebDriver driver=new FirefoxDriver(); 
Thread.sleep(3000); 
driver.get("https://www.google.com/gmail/about/"); 
driver.manage().timeouts().pageLoadTimeout(40,TimeUnit.SECONDS);     
//Clicking on Create account link
driver.findElement(By.xpath("//a[@href='https://accounts.google.com/SignUp?service=mail&continue=http%3A%2F%2Fmail.google.com%2Fmail%2F%3Fpc%3Dcarousel-about-en']")).click(); 
driver.manage().timeouts().pageLoadTimeout(40,TimeUnit.SECONDS); 
Assert.assertTrue(driver.findElement(By.xpath("//input[@name='firstName']")).isDisplayed()); 

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: //input[@name='firstName']

how to solve this?

Guy
  • 46,488
  • 10
  • 44
  • 88

2 Answers2

0

It looks like your xpath might be wrong. Instead of:

//input[@name='firstName'] 

Try using:

//*[@id='firstName'] 

as the xpath.

  • You can find the xpath on Chrome by going to Inspect Element, right clicking the element and choosing the Copy XPath option from the "Copy" sub-menu.
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Sean42
  • 1
0

It's timing issue, the form takes a second or two to load. You can add explicit wait to wait for it

WebDriverWait wait = new WebDriverWait(WebDriverRefrence, 10);
WebElement firstName = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("firstName")));
Assert.assertTrue(firstName.isDisplayed()); 
Guy
  • 46,488
  • 10
  • 44
  • 88
  • I have tried using explicitwait ..Still throwing TimeoutException Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for presence of element located by: By.xpath: //*[id='firstName'] (tried for 15 second(s) with 500 MILLISECONDS interval) at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:232) at srcpkg.Registration.main(Registration.java:35) – Pradeep Dec 10 '18 at 07:14
  • @Pradeep `//*[id='firstName']` is not valid `xpath`. Use `//*[@id='firstName']` or `By.id("firstName")` – Guy Dec 10 '18 at 07:41