Xpath Correct but still get no such element: Unable to locate element:

21,417 views
Skip to first unread message

Ashish Srivastava

unread,
Sep 18, 2016, 11:27:54 PM9/18/16
to Selenium Users

My Xpath is correct & no iFrame and I can locate element in Chrome console but my program still fails. I have used explicit wait also.

website http://newtours.demoaut.com/ I am trying to locate login page and send login id.



Error:
PASSED: openURL FAILED: loginToTours **org.openqa.selenium.NoSuchElementException**: **no such element: Unable to locate element: {"method":"xpath","selector":"//input[@name='userName']"}** *** Element info: {Using=xpath, value=//input[@name='userName']}



package SeleniumPracticePackage;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class CallUrl {
WebDriver driver;
Properties prop;
@BeforeTest
public void openBrowser() throws IOException
{
// driver = new ChromeDriver();
 
ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches","--disable-extensions");
System.setProperty("webdriver.chrome.driver","C:\\Users\\Ashish\\Documents\\Selenium\\drivers\\chromedriver_win32\\chromedriver.exe");
//System.setProperty("webdriver.chrome.driver",(System.getProperty("user.dir") + "//src//test//resources//chromedriver_new.exe"));
driver = new ChromeDriver(options);  
}
@Test
public void openURL() throws IOException
{
//call URL from properties file
prop = new Properties();
FileInputStream urlFile = new FileInputStream("C:\\Users\\Ashish\\Documents\\Selenium\\SeleniumPracticeSite\\src\\URL.properties");
prop.load(urlFile);
driver.get(prop.getProperty("URL"));
WebDriverWait myDynamicElement = new WebDriverWait(driver,30);
myDynamicElement.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='userName']")));
}
@Test
public void loginToTours () throws InterruptedException
{
  driver.findElement(By.xpath("//input[@name='userName']")).click();
}
}

HTML source

       <tr>
                  <td align="right"><font face="Arial, Helvetica, sans-serif" size="2">User 
                    Name: </font></td>
                  <td width="112">
                    <input type="text" name="userName" size="10">
                  </td>
</tr>


Unable to find any answers to my post in stackoverlfow

⇜Krishnan Mahadevan⇝

unread,
Sep 18, 2016, 11:50:23 PM9/18/16
to Selenium Users
Ashish,

I have responded to your query on StackOverFlow.


That should resolve your issue. The problem is in your test code.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/8e790b8a-7e32-401d-84d8-6217c2d4f351%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Shubham Agarwal

unread,
Sep 26, 2016, 5:57:28 AM9/26/16
to Selenium Users
Change the xpath and then try again

priy...@ask-cs.com

unread,
Mar 14, 2017, 12:04:31 PM3/14/17
to Selenium Users
Hi Ashish,

My xpath is .//*[@id='9f410057-bfb4-ec00-77f4-a4e4bb9d628c'] 
This is displayed when i try to find it using firebug but not able to run it with this as it gives the 'No such element error'. 

DarthOpto

unread,
Mar 14, 2017, 1:15:37 PM3/14/17
to Selenium Users
In addition to the other answers, I would definitely consider not looking by id in this instance. If the id is ```.//*[@id='9f410057-bfb4-ec00-77f4-a4e4bb9d628c``` more than likely this ID is dynamic and will change a lot. Best to avoid the headache altogether and find a better selector.  

Doug Dragon

unread,
Mar 14, 2017, 1:16:20 PM3/14/17
to Selenium Users
It might be easier to use CSS and reference the name attribute instead of using XPATH. In this case, you could do something like this:

'input[name=userName]'

If you must use XPATH, please disregard my answer.
Thanks,

-Doug

A.phani chakravarthy

unread,
Mar 14, 2017, 2:36:00 PM3/14/17
to Selenium Users
Hi Ashish !

The following code should work for you.I have tried the code in the URL specified by you above and its working fine.And please make sure you have latest chrome driver in you system.
WebDriver dr=new ChromeDriver();
Thread.sleep(1000);
WebElement username=dr.findElement(By.name("userName"));
username.sendKeys("username");
WebElement password=dr.findElement(By.name("password"));
password.sendKeys("password");
Thread.sleep(1000);
dr.findElement(By.name("login")).click();

Kundana Devi

unread,
Mar 15, 2017, 4:40:38 AM3/15/17
to seleniu...@googlegroups.com
hi Ashish,

The above code will work.Alo,I feel that when textbox is being used as a web-element its better to use senkeys method rather than click.


Regards,
Kundana

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages