6

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"}

5 Answers 5

7

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

1
  • 1
    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 Commented Jan 17, 2012 at 20:18
2

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

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?
2
  • +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. Commented Jan 17, 2012 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
    Commented Jan 18, 2012 at 22:35
1

This selenium webdriver tutorial explains how to click a link by its href: http://www.testingexcellence.com/click-link-href-value-webdriver/

1
  • it's ok to refer someone to another web page, but you should also try to answer the question without requiring someone to click on the link.
    – user246
    Commented Mar 21, 2016 at 17:16
0

Try either:

driver.find_element_by_xpath(u'//a[text()="Foo text"]')

or dedicated driver method:

driver.find_element_by_link_text("Foo text")

Add .click() if needed.

For troubleshooting, check this post at SO, as maybe your page isn't loaded yet, so you probably need to write a wait wrapper to wait for an element to appear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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