How to wait until the count of a web element changes in webdriver?



  • I'm new to selenium. I am facing an issue. The number of web elements in a page increases on a button click(triggered by ajax call). So while getting the size of the element after button click gives the same value.(But it was updated in site manually). I have tried waiting for ajax, and also implicit wait. On implicit wait it throws element not found exception. How can I wait until the count of the web element gets changed.



  • Thanks for your responses. But this is the script I'm using to resolve this issue public void waitUntilCountChanges() { WebDriverWait wait = new WebDriverWait(getDriver(), 5); wait.until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver) { int elementCount = driver.findElement(By.xpath("xxxx")).size(); if (elementCount > 1) return true; else return false; } }); } Or in Python class elements_length_changes(object): """An expectation for checking that an elements has changes. locator - used to find the element returns the WebElement once the length has changed """ def init(self, locator, length): self.locator = locator self.length = length def call(self, driver): element = driver.find_elements(*self.locator) element_count = len(element) if element_count > self.length: return element else: return False locator = (By.XPATH, "//a[@aria-label='Home']") length = len(self.wd.find_elements_by_xpath("//a[@aria-label='Home']") condition = elements_length_changes(locator, length) WebDriverWait(driver, 5).until(condition)



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 3
  • 2
  • 2
  • 2