【问题标题】:Can we have a generic wait method using ExpectedConditions and FluentWait in selenium?我们可以在 selenium 中使用 ExpectedConditions 和 FluentWait 的通用等待方法吗?
【发布时间】:2013-11-01 11:59:07
【问题描述】:

我们能不能有一个通用的等待方法...即(1.等待页面加载 2.找到元素 3.如果没有找到刷新),这一直持续到 fluentwait 超时。

【问题讨论】:

  • 当然可以,但是您自己研究过吗?第一步是对其进行设置,使其仅尝试查找元素,然后对其进行修改,以便如果找不到该元素,则仅刷新页面。完成。
  • 我实际上的意思是三个步骤(1.等待页面加载 2.查找元素 3.如果未找到刷新)会进行多次,因此每次刷新后 1 和 2 再次运行。跨度>
  • 好的,但是您尝试过什么?你已经知道你正在处理的类。您已经熟悉查找元素。这里有什么问题?您在实施哪些方面有困难?

标签: java selenium selenium-webdriver selenium-grid


【解决方案1】:

您是否有需要这样做的特殊业务案例?如果你不这样做,你可以在设置驱动程序对象时设置一个隐式等待。在 c# 中,它看起来像:

myDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

很多人会说你应该避免隐式等待,但是如果你觉得你需要去构建一个方便的方法,那么隐式等待可能更可取。

【讨论】:

    【解决方案2】:

    您可以对所有使用 WebdriverWait -(1.等待页面加载 2.查找元素 3.如果未找到刷新)

       WebDriverWait wait = new WebDriverWait(driver, 18); // Times Out after 18 Seconds
        PageUtil.refreshObject(driver, By.linkText("Element")); // refresh the Element
        wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Element"))); // wait till  Element is Enabled and Visible before Clicking on that Element
    
        /**
        *Pass the Control to that Element and Click on that Element (preferred in IE)
        *
        */
        WebElement element = driver.findElement(By.linkText("Element"));
        element.sendKeys(org.openqa.selenium.Keys.CONTROL);
        element.click();
    

    这是 RefreshObject 方法,PageUtil 是 ClassName

    public static WebElement refreshObject(WebDriver driver, By locator) {
            int counter = 0;
    
            try {
                counter = counter + 1;
    
                return driver.findElement(locator);
            }
    
            catch (StaleElementReferenceException e) {
    
                return refreshObject(driver, locator);
    
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 2014-01-28
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多