如何使用Java在Selenium Webdriver中使用clickandwait?

时间:2013-11-18 09:04:09

标签: selenium selenium-webdriver

我试图点击该对象,以显示值弹出窗口列表。

我使用了以下脚本,但无法填充列表值弹出窗口。

Driver.findElement(By.xpath(OR.getProperty(Object))).click();
   Thread.sleep(1000L);

还有其他方法可以点击并等待对象吗?

4 个答案:

答案 0 :(得分:12)

Driver.findElement(By.xpath(OR.getProperty(Object))).click();
WebDriverWait wait = new WebDriverWait(Driver, timeoutInSeconds);
wait.until(ExpectedConditions.elementToBeClickable(By.id("yourId"))); 

还有其他条件,例如visibilityOf,请选择最适合您的条件 - documentation here

答案 1 :(得分:2)

            String mainWindowHandle = driver.getWindowHandle();
            System.out.println(mainWindowHandle);

           wait.until(ExpectedConditions.elementToBeClickable(By.xpath(OR.getProperty(Object)));
            Driver.findElement(By.xpath(OR.getProperty(Object))).click();

            PageUtil.sleep(3000);
            Set<String> s1 = driver.getWindowHandles();

            Iterator<String> ite = s1.iterator();
            while (ite.hasNext()) {
                String popupHandle = ite.next().toString();
                System.out.println(popupHandle + " Present Pop Up window name");
                if (!popupHandle.contains(mainWindowHandle)) {
                    driver.switchTo().window(popupHandle);
                }
            }


WebDriverWait wait = new WebDriverWait(Driver, timeoutInSeconds);
wait.until(ExpectedConditions.elementToBeClickable(By.id("yourId"))); 
Driver.findElement(By.id("yourId")).click();

driver.switchTo().window(mainWindowHandle);

答案 2 :(得分:0)

点击后,您可以等待使用FluentWait,

// Waiting 30 seconds for an element to be present on the page, checking
   // for its presence once every 5 seconds.
   Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(NoSuchElementException.class);

   WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
     public WebElement apply(WebDriver driver) {
       return driver.findElement(By.id("foo"));
     }
   });

refer this for more info

答案 3 :(得分:0)

我不太熟悉java语法,但我会给你php代码,你可以写类似的。我们的想法是按标签查找HTML元素,并在您点击之前获取它的ID。然后在点击之后等待id改变。

看起来像这样:

class MySeleniumCondition extends WebDriverExpectedCondition{
  public static function htmlElementIdIsNot($htmlElementId) {
    return new WebDriverExpectedCondition(
      function ($driver) use ($htmlElementId) {
        $htmlElement = $driver->findElement(WebDriverBy::tagName('html'));
        return $htmlElementId != $htmlElement->getId();
      }
    );
  }
}

  // .............. somehere in a class:

  public function waitForNewPage($oldHtmlElementId, $time = 10){
    try{
      $this->driver->wait($time)->until(
        MySeleniumCondition::htmlElementIdIsNot(
          $oldHtmlElementId
        )
      );
      return true;
    }catch(TimeOutException $e){
      return false;
    }
  }

  // use this only when you are sure the page will reload
  public function clickAndWait(WebDriverBy $locator, $time = 10){
    $webElement = $this->driver->findElement($locator);
    $oldHtmlElement = $driver->findElement(WebDriverBy::tagName('html'));
    $oldHtmlElementId = $oldHtmlElement->getId();
    $webElement->click();
    $this->waitForNewPage($oldHtmlElementId, $time);
    return true;
  }