0

This is my first Question here, so please bare :)

I am trying to get the Selenium Webdriver to wait until an Element is visible, if not it should use the else.

This is my code, throwing system.argumentexception the path is not of a legal form.

if (driver.FindElement(By.Id("ember20"), timeout).Displayed)
{
           doXX();
}
else if (driver.FindElement(By.Id("ember19"), timeout).Displayed)
{
          doXX2();
}
    public static class WebDriverExtensions
    {
        public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)
        {
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
            return wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(by)).FindElement(by);
        }
    }
DebanjanB
  • 118,661
  • 30
  • 168
  • 217

3 Answers3

0

ArgumentException Class

ArgumentException is thrown when one of the arguments provided to a method is not valid. Details:

[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class ArgumentException : SystemException

FindElement Method

FindElement() method takes only single argument and is defined as follows:

IWebElement FindElement(
    By by
)

Parameters:
    by
        Type: OpenQA.Selenium.By
        The locating mechanism to use.

Return Value:
    Type: IWebElement
    The first matching IWebElement on the current context.

This usecase

In your code trials you have attempted to send 2 arguments to FindElement() method as follows:

if (driver.FindElement(By.Id("ember20"), timeout).Displayed)

Hence you see system.argumentexception

An ideal way would had been to send only the By parameter as an argument as follows:

if (driver.FindElement(By.Id("ember20")).Displayed)

However, this line of code would also have raised NoSuchElementException as the Id attribute ember20 is dynamically generated through Ember.js and the numeric part would be dynamic.


Solution

A canonical approach would have been to lookout for the visibility of the element inducing WebDriverWait for the ExpectedConditions as ElementIsVisible() using either of the the following Locator Strategies:

  • CssSelector:

    IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("CssSelector_identifying_the_element_uniquely")));
    
  • XPath:

    IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("XPath_identifying_the_element_uniquely")));
    

Additional Consideration

Ensure that:

  • ChromeDriver is updated to current ChromeDriver v79.0 level.
  • Chrome is updated to current Chrome Version 79.0 level. (as per ChromeDriver v79.0 release notes)
  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.

Reference

You can find a relevant discussion in:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
0

I encountered the same problem, in my case the nuget-package Costura was the problem. The package bundles all referenced .dlls and embeds them into the exe.

I removed all dependencies and uninstalled the package, now it's finally working!

MIDARE
  • 1
-1

It seems that the seleniumwebdriver is not on correct path. Please check that selenium webdriver and support dlls are on correct path.

Muzzamil
  • 2,419
  • 2
  • 7
  • 21