Rajesh S
Rajesh S

Reputation: 191

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

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:

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();

Upvotes: 1

Views: 16173

Answers (6)

Alejandro
Alejandro

Reputation: 122

I recommend after triggering any kind of web method example: click, keyboardType, etc.. use a time.sleep(1) this will give enough time to the browser to load the concatenated, new or loaded DOM added to the tree. If you dont give enough time it will be undefined.

Note: this code is used in python, use the equivalent code for java.

Upvotes: 0

bugCracker
bugCracker

Reputation: 3796

1) First use wait command before enter text

2) Do not use "id" locator because its same for both

3) Use "class" locator instead, for both input element.

Upvotes: 1

user2323037
user2323037

Reputation: 21

The error is in between these two lines means PD_Name.submit(); WebElement PD_Age = driver.findElement(By.name("i_passengerAge"));

After submitting you have to wait till entire page gets load. According to this you have to modify the script and write waitForElementPresent(element) function.

In this case you script will be:enter code here

PD_Name.submit();
waitForElementPresent(i_passengerAge);
WebElement PD_Age = driver.findElement(By.name("i_passengerAge"));

Try this and let me know if its works or not.

Thanks, Krishna.

Upvotes: 2

Nashibukasan
Nashibukasan

Reputation: 2028

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.

Upvotes: 2

Harshal Kakade
Harshal Kakade

Reputation: 21

Can you please check is this lies in a frame as HemChe said earlier, if it so you need to switch to that particular frame.

Upvotes: 1

e1che
e1che

Reputation: 1251

I hope you've tried to get the element with By.id, By.cssSelector or By.xpath.

Watch out about the submit() method. Because it search automaticaly the form to send and sometimes it may fail. That's why i prefere the good old click().

By.id :

WebElement PD_Name=driver.findElement(By.id("i_passengerName"));
PD_Name.sendKeys("Testing");
PD_Name.click();

By.xpath :

WebElement PD_Name=driver.findElement(By.xpath("//input[@name='i_passengerName']"));
PD_Name.sendKeys("Testing");
PD_Name.click();

For the cssSelector way, you can take a look here.

Tell me if it improved. =)

EDIT :

Actually, there is no i_passengerGender

Upvotes: 1

Related Questions