Python Selenium Do-While 循环
Python Selenium Do-While Loop
我正在尝试使用以下代码完成一个 do-while 循环。我们正在等待处理报告,它只会在完成并单击检索按钮后显示在网页上。代码,
# 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 ###
如果最终变得可见的元素最初没有出现在页面上,您的代码可以是这样的:
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
5 秒和 20 秒的硬编码睡眠看起来不太好。如果可能的话,应该通过预期条件显式等待来更改它们
我正在尝试使用以下代码完成一个 do-while 循环。我们正在等待处理报告,它只会在完成并单击检索按钮后显示在网页上。代码,
# 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 ###
如果最终变得可见的元素最初没有出现在页面上,您的代码可以是这样的:
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
5 秒和 20 秒的硬编码睡眠看起来不太好。如果可能的话,应该通过预期条件显式等待来更改它们