【问题标题】:Implicit wait Command Not Working-selenium webdriver C#隐式等待命令不起作用-selenium webdriver C#
【发布时间】:2018-03-27 19:11:58
【问题描述】:

伙计们,我已经开始研究 selenium 网络驱动程序了。你可以假设我是初学者。目前,我在代码(C#)中实现隐式等待命令时遇到了困难。它没有按应有的方式工作,并且由于未找到 Element 而导致异常,但是当我添加“Thread.Sleep(3000) 时,代码可以完美执行。我一直在互联网上寻找解决方案,但无法解决问题。下面我提到了示例代码。

class Entrypoint
{

static void Main()

{
 IWebDriver driver = new ChromeDriver();
    **driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);**
    driver.Navigate().GoToUrl("https://r1.netrevelation.com:8443/mcba-cms/pages/flight-transfer.cab");
    driver.Manage().Window.Maximize();


    driver.FindElement(By.Id("loginlink")).Click();
    driver.FindElement(By.Id("headerSubView:inputUserName:input")).SendKeys("st001");
    driver.FindElement(By.Id("headerSubView:inputPassword:input")).SendKeys("hello321" + Keys.Enter);

    driver.FindElement(By.Id("dateOfFlight:input")).Click();**//This Step does not get Executed , it throws exception element not found.**
    driver.FindElement(By.Id("ui-datepicker-div")).Click(); 
    driver.FindElement(By.XPath(".//*[@id='ui-datepicker-div']/div/a[2]/span")).Click(); 
    driver.FindElement(By.LinkText("28")).Click(); 
    IWebElement Flightno = driver.FindElement(By.Id("selectedFlight:input"));
    Flightno.SendKeys("BA901" + Keys.Enter);
    IWebElement Flighttick = driver.FindElement(By.Id("flightTickImg"));


    driver.Quit();

请注意,目前我不想使用显式等待,因为隐式将满足我的需要(如果它开始工作)。上面的代码以超音速运行,以某种方式设法登录到系统,但之后每次失败,原因是一旦发出登录请求,系统暂停 2-3 秒。请提供您在这方面的评论。

【问题讨论】:

    标签: c# selenium webdriver wait implicit


    【解决方案1】:

    根据documentationImplicit Wait 是告诉WebDriver 在尝试find an element()find all elements() 时轮询HTML DOM 一段时间(如果它们不能立即可用)。但是 DOM Tree 中元素的可用性并不能保证 ElementToBeClickable 就像您在代码块中尝试的那样。因此,您将 exception 视为 Element not found


    解决方案:

    因此,解决您的问题的方法是诱导 Explicit WaitWebDriverWaitExpectedConditions 子句作为 ElementToBeClickable 这不仅会确认 HTML DOM 中元素的可用性,而且还要确保元素是可点击的,即元素已显示并启用,如下所示:

    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("loginlink")));
    

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 2022-06-22
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 2018-01-24
      相关资源
      最近更新 更多