0

I have created a small test using C# in Selenium Webdriver, for this test I first used Selenium IDE and then converted that IDE code to C# code and created Project in VSTS C#. But while running the same I am getting an error.

An unhandled exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll

This error occurs at this point

Assert.AreEqual("My Timesheet", driver.FindElement(By.LinkText("My Timesheet")).Text);

I have searched the same over StackExchange but not able to get the exact solution.

Link 1, Link 2, Link 3

HTML is

<a id="" href="#null" title="" onclick="getTimesheet(this, 1, true)">My Timesheet</a>
  • And the HTML? Seems the By.LinkText does not exist, findelement alsways throws a NoSuchElementException and stops the code. – Niels van Reijmersdal Aug 7 '15 at 14:47
  • HTML content added – Dhiman Aug 7 '15 at 15:02
2

You could try a couple of things..

Does it work using a different selector, e.g: By.XPath("//a[text()='My Timesheet']") ?

If that doesn't work either, selenium may be trying to get the element before it has finished loading in the DOM. So we need to wait and retry to get the element.

You can do that by using Webdriver implicit waits, or maybe implementing something like the below extension method on IWebDriver:

public static IWebElement GetElement(this IWebDriver driver, By selector, int tries = 10)
{
    for (int i = 1; i <= tries; i++)
    {
        try
        {
            return driver.FindElement(selector);
        }
        catch (WebDriverException)
        {
            System.Threading.Thread.Sleep(200);
        }
    }
    return null;
}

If the element is still not found, you could increase the number or retries. Sounds possible that your DOM may be taking a long time to populate.

This will override WebDriver's normal behaviour, to return null if an element is not found, so be sure to carry out null checking after using it if you do.

|improve this answer|||||
  • Thanks for the solution, but my issue got resolved with the use of Frames, actually some elements which were throwing errors where in different frames and those frames were not having any Name and ID, I randomly used the Frame number method. – Dhiman Aug 24 '15 at 12:58
  • @Dhiman - Could you post your solution as an answer? – Lyndon Vrooman Aug 24 '15 at 19:23
  • Same has been posted – Dhiman Aug 24 '15 at 19:33
0

I have used this and it worked for me:

driver.SwitchTo().Frame(1);            
try
{
   Assert.AreEqual("My Next Question", driver.FindElement(By.XPath(".//*........")).Text);

}
catch (AssertionException)
{
  // verificationErrors.Append("Element was not displayed");
}

string element = driver.FindElement(By.XPath(".//*[@id='request........")).Text;
Console.Write("Number of pending requests are: " + element);
System.IO.StreamWriter file = new System.IO.StreamWriter(@"E:\New\test.txt");
file.WriteLine("Number of pending requests are: " + element);
file.Close();
driver.FindElement(By.LinkText("Pending")).Click();            
driver.SwitchTo().DefaultContent();
driver.FindElement(By.Id("logout-link")).Click();
driver.Quit();
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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