【问题标题】:Explicit wait exception c# seleniumc# selenium 显式等待异常
【发布时间】:2018-11-30 11:45:24
【问题描述】:

我不熟悉在我的代码中使用显式等待。在我的代码中,我以前有隐式等待,但现在我想使用显式等待。我尝试使用与隐式等待相同的概念,但我的SelectElementFromDropDown 出现异常。该错误是专门用“国家”和“货币”引发的

我已经注释掉了我之前工作的代码。

我已经按照这个例子进行了

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


    }
}

【问题讨论】:

  • 错误是什么? TimeoutException?
  • 未指定国家和货币值。

标签: c# selenium selenium-webdriver


【解决方案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();

    }

【讨论】:

  • 当 'WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5))' 用于货币 'wait' 时抛出异常,因为范围内已经定义了等待
  • 注释掉第二个等待
  • 更新了答案
猜你喜欢
  • 2023-03-11
  • 2018-05-20
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 2018-02-12
相关资源
最近更新 更多