0

I'm using selenium C# chrome driver to access some website and download data from it. This is a sample code of my application.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        try
        {
            AppSettingsReader configReader = new AppSettingsReader();
            using (var driver = new ChromeDriver(HostingEnvironment.ApplicationPhysicalPath)) //here I used latest chrome driver(V 2.42.59)
            {
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
                //driver.Manage().Timeouts().PageLoad = TimeSpan.FromMinutes(2);

                driver.Url = "https://crims.crib.lk";
                var userNameField = driver.FindElementById("txtUserName");
                var userPasswordField = driver.FindElementById("txtPassword");
                driver.Quit();
            }
        }
        catch (Exception ex)
        {
            Logger.LogWriter(" WebApplication2.Controllers", ex, "HomeController", "Index");
        }

        return View();
    }
}

This code is working properly when I execute code through VS. After publishing the application through IIS server, Driver does not navigate to the URL. Following error showing in the log file.

Source :WebApplication2
Module:HomeController
Method:Index
Message :no such element: Unable to locate element:
{"method":"id","selector":"txtUserName"}
  (Session info: chrome=69.0.3497.100)
  (Driver info: chromedriver=2.42.591088
(7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT
6.2.9200 x86_64)
StackTrace :   at
OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response
errorResponse)

This same error occurred in VS when the browser does not navigate to URL properly. How to solve this problem. And keep note I installed following NuGet packages successfully. I'm wroking on windows server 2012 PC. I hosted my app its own IIS

NuGet Packages: RC | WebDriver | WebDriverBackedSelenium | Support

Thanks in advance.

Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68

1 Answers1

1

This could happen for a multiple of reasons. You need to implement a wait and poll mechanism

This is one way of doing it:

var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 60));
var element = wait.Until(condition =>
{
    try
    {
        var elementToBeDisplayed = driver.FindElement(By.Id("content-section"));
        return elementToBeDisplayed.Displayed;
    }
    catch (StaleElementReferenceException)
    {
        return false;
    }
    catch (NoSuchElementException)
    {
        return false;
    }
});

You can also use the FluentWait Command and ExpectedConditions example in this link. This is in java but you can convert that for c#

Aman B
  • 2,276
  • 1
  • 18
  • 26
  • Thanks, I tested your code, logs create till time out(mulitple logs), with previous error `Unable to locate element: {"method":"id","selector":"txtUserName"} `'' Finally this log created `Message :Timed out after 60 seconds` – Sachith Wickramaarachchi Sep 25 '18 at 11:00
  • Can't identify reason, internet conn is also works fine, – Sachith Wickramaarachchi Sep 25 '18 at 11:01
  • Try taking a [screenshot on failure](https://stackoverflow.com/questions/3346017/best-way-to-take-screenshots-of-tests-in-selenium-2) and see if you can figure out anything – Aman B Sep 25 '18 at 11:19
  • I'm working on windows server 2012 machine. And i hosted its IIS server.is that can be reason to occour this failier? Have any idea about that? – Sachith Wickramaarachchi Sep 25 '18 at 11:42