显式等待异常c#硒

时间:2018-06-21 08:09:51

标签: c# selenium selenium-webdriver

在代码中使用Explicit Wait是我的新手。在我的代码中,我以前有隐式等待,但是现在我想使用显式等待。我尝试使用与隐式等待相同的概念,但是SelectElementFromDropDown出现了异常。该错误专门用'country'和'currency'抛出

我已经注释掉了以前有效的代码。

我遵循了这个示例

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


    }
}

1 个答案:

答案 0 :(得分:1)

您将国家元素分配给变量按钮,将货币分配给变量元素。它应该分别是国家和货币。以下代码。

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

    }