Home > Enterprise >  Selenium wait until attribute appears
Selenium wait until attribute appears

Time:11-10

I'm using Selenium to download several files. The process is simple: I look for a reference, wait until results are ready and download the associated files.

I've a problem with the part "wait until results are ready". The website uses an AJAX table which loads the results. During the update of this table, an attribute appears in the HTML code and when results are ready it dissapears.

The object is always present, it's only the attribute that changes. If I do the next loop just right after clicking the button of search:

for i in range(0,10):
    print(self.driver.find_element(By.ID, "gridpoOverview").get_attribute("aria-busy"))
    time.sleep(0.05)

It returns (so I know how to detect it):

none 
true
true
none
none
none
none
none
none
none

I want to do it using an EC, but the next code doesn't work (Timeout exception):

WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@id="gridpoOverview" and @aria-busy="true"]')))

CodePudding user response:

Seems you are close enough.

As returned by get_attribute("aria-busy") the value of aria-busy attribute turns True.

So you need to replace true with True as follows:

WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@id="gridpoOverview" and @aria-busy="True"]')))

Update

As per your updated question you can use:

WebDriverWait(driver, 10).until(EC.staleness_of(driver.find_element_by_xpath("//div[@id="gridpoOverview" and not(@aria-busy)]")))

Or

WebDriverWait(driver, 10).until(EC.staleness_of(driver.find_element(By.XPATH, "//div[@id="gridpoOverview" and not(@aria-busy)]")))

CodePudding user response:

I've found a solution that works, but I don't know why.

The attribute 'aria-disabled' from the same element is always present. If I include this line before, it will work.

    WebDriverWait(self.driver, 20).until(EC.element_attribute_to_include((By.XPATH, '//div[@id="gridpoOverview"]'),'aria-disabled'))
    WebDriverWait(self.driver, 20).until(EC.element_attribute_to_include((By.XPATH, '//div[@id="gridpoOverview"]'),'aria-busy'))

If the first line if not present, it does not work (I've the Timeout exception).

Does anyone knows why ?

  • Related