J.dockster
J.dockster

Reputation: 43

Explicit wait exception c# selenium

I'm new to using Explicit wait within my code. In my code I previously had Implicit waits but I now want to use Explicit waits. I've tried to use the same concept as I had for Implicit waits but I get an exception for my SelectElementFromDropDown. The error is specifically thrown with 'country' and 'currency'

I've commented out my code that was previously working.

I've followed this example

    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
IWebElement button = wait.Until(ExpectedConditions.ElementExists(By.Id("someId"));

/

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using System.Threading;

namespace Exercise1
{
    class test3
    {

        static void Main(string[] args)
        {
            IWebDriver webDriver = new ChromeDriver();
            webDriver.Navigate().GoToUrl("http://www.asos.com/men/");
            webDriver.Manage().Window.Maximize();
            webDriver.FindElement(By.XPath(".//button[@data-testid='country-selector-btn']")).Click();

            WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
            IWebElement button = wait.Until(ExpectedConditions.ElementExists(By.Id("country")));
            SelectElementFromDropDown(country, "India");


            //webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            //IWebElement country = webDriver.FindElement(By.Id("country"));
            //SelectElementFromDropDown(country, "India");

            WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
            IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("currency")));
            SelectElementFromDropDown(currency, "$ USD");

            //IWebElement currency = webDriver.FindElement(By.Id("currency"));
            //SelectElementFromDropDown(currency, "$ USD");

            webDriver.FindElement(By.XPath(".//button[@data-testid='save-country-button']")).Click();

            webDriver.Quit();

        }

        private static void SelectElementFromDropDown(IWebElement ele, string text)
        {
            SelectElement select = new SelectElement(ele);
            select.SelectByText(text);
        }


    }
}

Upvotes: 0

Views: 230

Answers (1)

Murthi
Murthi

Reputation: 5347

You assigned country element to variable button and currency to variable element. It should be country and currency respectively. the following code.

 static void Main(string[] args)
 {
      IWebDriver webDriver = new ChromeDriver();
      webDriver.Navigate().GoToUrl("http://www.asos.com/men/");
      webDriver.Manage().Window.Maximize();
      webDriver.FindElement(By.XPath(".//button[@data-testid='country-selector-btn']")).Click();

      WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
        IWebElement country = wait.Until(ExpectedConditions.ElementExists(By.Id("country")));
        SelectElementFromDropDown(country, "India");


        //webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
        //IWebElement country = webDriver.FindElement(By.Id("country"));
        //SelectElementFromDropDown(country, "India");

        //WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
        IWebElement currency = wait.Until(ExpectedConditions.ElementExists(By.Id("currency")));
        SelectElementFromDropDown(currency, "$ USD");

        //IWebElement currency = webDriver.FindElement(By.Id("currency"));
        //SelectElementFromDropDown(currency, "$ USD");

        webDriver.FindElement(By.XPath(".//button[@data-testid='save-country-button']")).Click();

        webDriver.Quit();

    }

Upvotes: 1

Related Questions