12

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?

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.