11

I am trying to click on the object, to show the list of value pop-up.

I used the following script, but unable to populate the list value pop-up.

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

Is there any other way to click and wait for the object?

user2943890
  • 141
  • 1
  • 2
  • 7

4 Answers4

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

There are other conditions like visibilityOf so choose the one which suits you most - documentation here.

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
  • I have tried the above script, still my list value pop-up is not showing. Is there any other way to handle the pop-up. I have tried in Selenium IDE, it is working for the command "clickandwait". Please tell how to handle this in Selenium Webdriver. – user2943890 Nov 18 '13 at 10:59
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);
Satish
  • 724
  • 3
  • 10
  • 22
0

After clicking you can wait using 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

Amith
  • 6,818
  • 6
  • 34
  • 45
0

I'm not quite familiar with java syntax, but I'll give you php code and you can write similar. The idea is to find HTML element by tag and get it's id BEFORE YOU CLICK. Then AFTER THE CLICK wait for the id to change.

It looks like this:

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;
  }
NickSoft
  • 3,215
  • 5
  • 27
  • 48