Cordell Jones
Cordell Jones

Reputation: 93

Python Selenium Do-While Loop

I am trying to complete a do-while loop with the below code. We are waiting for a report to process, and it only shows on the webpage once completed and after clicking the retrieve button. The code,

# Go to list and click retrieve
driver.find_element(By.CSS_SELECTOR, "#j_idt40Lbl").click()
time.sleep(20) # takes a while for their side to run
retrieve = driver.find_element_by_css_selector("#tab_reportList\:listPgFrm\:listPgRetrBtn")
retrieve.click() ### Do this action ###
time.sleep(5)
retrieve.click()
time.sleep(5)
retrieve.click()
time.sleep(3)

# Click report file
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#tab_reportList\:listPgFrm\:listDataTable_0\:0\:reportLink"))).click() 
### Until this is visible ###

Upvotes: 2

Views: 394

Answers (1)

Prophet
Prophet

Reputation: 33351

In case the element finally becoming visible is not initially presents on the page your code can be something like this:

driver.find_element(By.CSS_SELECTOR, "#j_idt40Lbl").click()
time.sleep(20)
while True:
    driver.find_element_by_css_selector("#tab_reportList\:listPgFrm\:listPgRetrBtn").click()
    time.sleep(5)
    if driver.find_elements_by_css_selector("#tab_reportList\:listPgFrm\:listDataTable_0\:0\:reportLink"):
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#tab_reportList\:listPgFrm\:listDataTable_0\:0\:reportLink"))).click()
        break

Hardcoded sleeps of 5 and 20 seconds are not looking good. If possibly they should be changed by Expected Conditions explicit waits

Upvotes: 3

Related Questions