Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

I'm using Selenium 2.0 web driver. My script keeps failing whenever I try locating something in my page. It throws an exception whether I locate the element by LinkText or by XPath.

driver.FindElement(By.LinkText("Products")).Click();

Selenium.ProductPricing.TheUntitledTest:
OpenQA.Selenium.NoSuchElementException : Unable to locate element: {"method":"link text","selector":"Products"}

driver.FindElement(By.XPath("//div[@id='nav']/ul/li[1]/a")).Click();

Selenium.ProductPricing.TheUntitledTest:
OpenQA.Selenium.NoSuchElementException : Unable to locate element: {"method":"xpath","selector":"//div[@id='nav']/ul/li[0]/a"}
share|improve this question
add comment

3 Answers

A few things to look at.

  1. Are you sure that the LinkText is correct? Are you missing spaces, non breaking spaces, etc that may appear in the html but you don't see as a user?
  2. Are you sure the xpath is correct?
  3. Is this a timing issue? Are you trying to find the elements before the page is finished loading or before those elements are loaded?
share|improve this answer
    
+1 for you my friend.. however I found the issue. Perhaps you can help me find a better alternative to my solution. It was related to the waiting. –  MacGyver Jan 17 '12 at 20:20
1  
Personally, I don't even like Selenium's wait functions, they seem too fragile and I have to be explicit in what I want to wait for. In my selenium wrapper classes I implemented polling/retry logic on every action, so I can navigate to a new page and immediately call a click method on an element I know won't appear for another 10 seconds and it will try and retry up to a specified amount of time and eventually either work or time out. This has avoided so many headaches for me. –  Sam Woods Jan 18 '12 at 22:35
add comment

XPath list indexes are one-based, not zero-based. Try li[1].

share|improve this answer
    
thanks.. that's good to know... +1 ... I was testing with this wait when I had the linkText, so I'll correct that (good catch) ==> driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); ... my actual issue seems to be related to this waiting –  MacGyver Jan 17 '12 at 20:18
add comment
up vote 0 down vote accepted

Seems like I have to use this line of code before I invoke any click events. The "clickAndWait" conversion of the Selenium IDE to Webdriver doesn't seem to work properly. It converts everything to *.Click(). Adding the implicit wait after every click event and setting WAIT_TIME to 3 in my Constants class seems to do the trick.

driver.FindElement(By.CssSelector("input[type=\"submit\"]")).Click();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(Constants.WAIT_TIME));
driver.FindElement(By.LinkText("Products")).Click();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(Constants.WAIT_TIME));
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.