Unable to locate an element using Selenium WebDriver 2.31 (JRE 7)

selenium-webdriverwebdriver

I'm trying to automate a scenario which books a bus ticket in a website. I'm using Selenium WebDriver with Eclipse and when i try to locate the element, i.e. 'Passenger name', no compilation errors, but while executing it shows an error such as "Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//input[@name='i_passengerAge']"}".

HTML and Java code below, also i need an hasty solution for this.

My System Info:

  • Windows 7
  • Selenium WebDriver 2.31
  • Eclipse FW

HTML of the WebPage:

<input name="i_passengerName" id="i_passengerName" maxlength="30" class="inputclass pageRequired commonInputStyle" title="Please enter your name!" type="text">

<input name="i_passengerAge" id="i_passengerAge" maxlength="2" size="4" class="inputclass fillAge digits commonInputStyle" type="text">

My Automation Script:

WebElement PD_Name = driver.findElement(By.name("i_passengerName"));
PD_Name.sendKeys(new String[] {"Testing"});
PD_Name.submit();

WebElement PD_Age = driver.findElement(By.name("i_passengerAge"));
PD_Age.sendKeys(new String[] {"45"});
PD_Age.submit();

Best Answer

I myself am using C# so you will have to check the syntax here, but I believe your issue might be that you are accessing the elements too quickly (prior to the page fully loading). I would suggest trying the WebDriverWait class. I believe in Java it is something like:

WebDriverWait wait = new WebDriverWait(_driver, timeout);
wait.until(_driver.findElement(By.name("i_passengerAge")));

After this line you can then safely access the element as you have confirmed it has fully loaded on the page.

Related Topic