0

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);
        }


    }
}
2
  • What is the error? TimeoutException? Commented Jun 21, 2018 at 8:19
  • country and currency values are not assigned. Commented Jun 21, 2018 at 8:20

1 Answer 1

1

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();

    }
Sign up to request clarification or add additional context in comments.

3 Comments

When ' WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5))' is used for currency 'wait' throws an exception as wait is already defined in the scope
Comments out second wait then
Updated the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.