OpenQA.Selenium.NoSuchElementException : Unable to locate element

2,608 views
Skip to first unread message

Todd

unread,
Jul 22, 2011, 9:50:07 AM7/22/11
to NUnit-Discuss
Hi. I'm new to NUnit. I'm running version 2.5.10.11092 on Win 7, 32-
bit. When I try to run a test I get...

SeleniumTests.GoogleTestExportedInCSharpWebDriver.TheGoogleTestExportedInCSharpWebDriverTest:
OpenQA.Selenium.NoSuchElementException : Unable to locate element:
{"method":"css selector","selector":"#gb_2 > span.gbts"}

at
OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response
errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(DriverCommand
driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String
mechanism, String value)
at
OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByCssSelector(String
cssSelector)
at
OpenQA.Selenium.By.<>c__DisplayClass1e.<CssSelector>b__1c(ISearchContext
context)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
at
SeleniumTests.GoogleTestExportedInCSharpWebDriver.TheGoogleTestExportedInCSharpWebDriverTest()

Here's what I did:
- used Selenium IDE 1.1.0 to create a test, which runs and passes in
Se IDE
- exported the test as C# (WebDriver)
- copied/pasted the C# code into a Visual C# 2010 Express project
- added references to the required dll's
- ensured that there were no errors reported in the code
- built a dll assembly
- put the dll into a folder with the referenced dll's (verified that
the nunit.framework.dll was in this folder)
- created a new NUnit project and added the dll assembly from C#
Express
- clicked Run

The Firefox browser launches, but then the test fails with the message
above. Here is the code that was exported by Se IDE...

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace SeleniumTests
{
[TestFixture]
public class GoogleTestExportedInCSharpWebDriver
{
private IWebDriver driver;
private StringBuilder verificationErrors;
private string baseURL;

[SetUp]
public void SetupTest()
{
driver = new FirefoxDriver();
baseURL = "http://www.google.com/";
verificationErrors = new StringBuilder();
}

[TearDown]
public void TeardownTest()
{
try
{
driver.Quit();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}

[Test]
public void TheGoogleTestExportedInCSharpWebDriverTest()
{
driver.Navigate().GoToUrl("/");
driver.FindElement(By.CssSelector("#gb_2 >
span.gbts")).Click();
driver.FindElement(By.CssSelector("#gb_12 >
span.gbts")).Click();
driver.FindElement(By.CssSelector("#gb_8 >
span.gbts")).Click();
driver.FindElement(By.Id("d_launch")).Click();
}
private bool IsElementPresent(By by)
{
try
{
driver.FindElement(by);
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
}
}

Charlie Poole

unread,
Jul 22, 2011, 12:56:25 PM7/22/11
to nunit-...@googlegroups.com
As far as I can see, NUnit has done what it should do. When Selenium
couldn't fine the element '#gb2 > span.gbts' it threw an exception and
NUnit reported it.

Is the generated code intended to be used as-is, or are you supposed
to edit the code to complete it? I'm not a Selenium expert, but it looks
to be incomplete.

Specifically...

* The string baseURL is never used.
* You navigate to "/"
* One test displays the contents of verificationErrors, but nothing is ever
added to it.
* There is a method IsElementPresent that's never called.

Charlie

> --
> You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
> To post to this group, send email to nunit-...@googlegroups.com.
> To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nunit-discuss?hl=en.
>
>

bcook

unread,
Jul 23, 2011, 10:31:43 AM7/23/11
to NUnit-Discuss
While I am familiar with Selenium, I haven't used the Selenium 2.0
WebDriver syntax. As Charlie points out, the baseUrl is not being
used. I suspect you'd need to navigate to the site before attempting
to drive it. The syntax to navigate would look like:

driver.Navigate().GoToUrl( baseUrl );

I'm curious, did you edit the output from Selenium IDE? That
development group is largely java centric and most of the C# syntax
examples that I've seen are incomplete. There could be a bug in the
IDE's C# WebDriver formatter.

Todd

unread,
Jul 26, 2011, 11:16:13 AM7/26/11
to NUnit-Discuss
I replaced "/" with "http://www.google.com/" and the test completed
and passed. I will let the Selenium folks know about this. Thanks for
the help!

On Jul 22, 12:56 pm, Charlie Poole <nunit...@gmail.com> wrote:
> As far as I can see, NUnit has done what it should do. When Selenium
> couldn't fine the element '#gb2 > span.gbts' it threw an exception and
> NUnit reported it.
>
> Is the generated code intended to be used as-is, or are you supposed
> to edit the code to complete it? I'm not a Selenium expert, but it looks
> to be incomplete.
>
> Specifically...
>
> * The string baseURL is never used.
> * You navigate to "/"
> * One test displays the contents of verificationErrors, but nothing is ever
> added to it.
> * There is a method IsElementPresent that's never called.
>
> Charlie
>
>
>
> On Fri, Jul 22, 2011 at 6:50 AM, Todd <tmtesttmt...@gmail.com> wrote:
> > Hi. I'm new to NUnit. I'm running version 2.5.10.11092 on Win 7, 32-
> > bit. When I try to run a test I get...
>
> > SeleniumTests.GoogleTestExportedInCSharpWebDriver.TheGoogleTestExportedInCS­harpWebDriverTest:
> > OpenQA.Selenium.NoSuchElementException : Unable to locate element:
> > {"method":"css selector","selector":"#gb_2 > span.gbts"}
>
> > at
> > OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response
> > errorResponse)
> > at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(DriverCommand
> > driverCommandToExecute, Dictionary`2 parameters)
> > at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String
> > mechanism, String value)
> > at
> > OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByCssSelector(String
> > cssSelector)
> > at
> > OpenQA.Selenium.By.<>c__DisplayClass1e.<CssSelector>b__1c(ISearchContext
> > context)
> > at OpenQA.Selenium.By.FindElement(ISearchContext context)
> > at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
> > at
> > SeleniumTests.GoogleTestExportedInCSharpWebDriver.TheGoogleTestExportedInCS­harpWebDriverTest()
> > For more options, visit this group athttp://groups.google.com/group/nunit-discuss?hl=en.- Hide quoted text -
>
> - Show quoted text -

Charlie Poole

unread,
May 13, 2013, 2:20:41 PM5/13/13
to NUnit-Discuss
You will be more likely to get an answer to that question on a list dedicated to Selenium.

Charlie


On Mon, May 13, 2013 at 6:59 PM, Shree <shreelax...@gmail.com> wrote:
I did exactly the same steps as Todd; however I am getting 'Unable to locate element' error even if element exists on UI. Is that still a bug in the Selenium Webdriver?
To unsubscribe from this group and stop receiving emails from it, send an email to nunit-discus...@googlegroups.com.

To post to this group, send email to nunit-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages