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>
2
  • And the HTML? Seems the By.LinkText does not exist, findelement alsways throws a NoSuchElementException and stops the code. Commented Aug 7, 2015 at 14:47
  • HTML content added
    – Dhiman
    Commented Aug 7, 2015 at 15:02

2 Answers 2

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.

3
  • 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
    Commented Aug 24, 2015 at 12:58
  • @Dhiman - Could you post your solution as an answer? Commented Aug 24, 2015 at 19:23
  • Same has been posted
    – Dhiman
    Commented Aug 24, 2015 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();

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.