0

I'm trying to use Selenium with version 74 of chrome. I downloaded the most current updates from Selenium and the ChromeDriver as instructed by the documentation. This was my functioning code before the upgrades -

IWebDriver driver = new ChromeDriver();

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(8));
{
    driver.Url = "https://iam.mySite.com";
};

but after the upgrade I began getting the error

OpenQA.Selenium.DriverServiceNotFoundException: 'The chromedriver.exe file 
does not exist in the current directory or in a directory on the PATH 
environment variable. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html

Because of that error, I changed my code to this

IWebDriver driver = new ChromeDriver(@"C:\Users\UserName\.nuget\packages\selenium.webdriver.chromedriver\74.0.3729.6\driver\win32");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(8));
{
    driver.Url = "https://iam.mySite.com";
};

But now I'm getting this error -

OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: 
{"method":"xpath","selector":"//*[@id='username']"}
  (Session info: chrome=74.0.3729.131)
  (Driver info: chromedriver=74.0.3729.6)

I double checked the username xpath and it hasn't changed. Is there an easy way to downgrade my chrome version? Am I putting the driver in the wrong folder?

C. Peck
  • 3,641
  • 3
  • 19
  • 36
KarkMump
  • 81
  • 1
  • 8
  • Is your browser launching properly? It seems like Selenium thinks it is. If so I can't imagine that your chrome version or its location is the problem. – C. Peck May 09 '19 at 05:47
  • You posted an exception message referring to a `.FindElement(By.XPath("//*[@id='username']"))` that isn't in your posted code. For us to be able to help you, you need to post an [mcve] and the error/exception message from running that code. Also, you are using NuGet, you don't need to specify the ChromeDriver path. – JeffC May 09 '19 at 15:33

1 Answers1

0

@karkMump, once You ensured that selenium starts fine and configuration is correct - try out different wait mechanisms like:

  • explicit waits
  • implicit waits
  • fluent wait (in java bindings)

and apply it to the webelement on the page.

Some code examples of waiting for some specific elemenents with C#:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(1));
IWebElement element = wait.Until<IWebElement>(ExpectedConditions.ElementIsVisible(By.TagName("textarea")));

Alternatively, if you need more control over the specific settings of the wait, you could do this:

DefaultWait<IWebDriver> wait = new DefaultWait(driver);
wait.Timeout = TimeSpan.FromSeconds(1);
wait.PollingInterval = TimeSpan.FromMilliseconds(100);
wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
IWebElement element = wait.Until<IWebElement>((d) =>
{
    return d.FindElement(By.TagName("textarea"));
});

If you really, really want to put this into a format where you can pass any locator you want, wrap that in another method, like so:

public IWebElement WaitForElementVisible(By locator, TimeSpan timeout)
{
    WebDriverWait wait = new WebDriverWait(driver, timeout);
    IWebElement element = wait.Until<IWebElement>(ExpectedConditions.ElementIsVisible(locator));
}

A note on C# specific about Default wait in c#: Default wait is more generic wait in the sense that it is not bound to WebDriver. This wait can be used to pass in any object to wait on. Here is the code example (of awaiting for change color of some specific element on the page):

static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("https://experitest.com/selenium-testing");
            IWebElement element = driver.FindElement(By.Id("colorVar"));
            DefaultWait<IWebElement> wait = new DefaultWait<IWebElement>(element);
            wait.Timeout = TimeSpan.FromMinutes(2);
            wait.PollingInterval = TimeSpan.FromMilliseconds(250);

            Func<IWebElement, bool> waiter = new Func<IWebElement, bool>((IWebElement ele) =>
                {
                    String styleAttrib = element.GetAttribute("style");
                    if (styleAttrib.Contains("red"))
                    {
                        return true;
                    }
                    Console.WriteLine("Color is still " + styleAttrib);
                     return false;
                });
            wait.Until(waiter);
        }

You can see that here DefaultWait is not tied to WebDriver. You can wait on anything you like

Hope this helps. Regards, Eugene

eugene.polschikov
  • 7,254
  • 2
  • 31
  • 44