简体   繁体   English

显式等待异常C#Selenium

[英]Explicit wait exception c# selenium

I'm new to using Explicit wait within my code. 我是在代码中使用Explicit Wait的新手。 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 . 我尝试使用与隐式等待相同的概念,但是我的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);
        }


    }
}

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM