简体   繁体   中英

Selenium ChromeDriver doesn't navigate URL after publishing

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.

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#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM