页面上的任务栏为空,我试图每3秒刷新一次页面以查找任务并尽快将其拾起,因为它可能会消失。
我之前尝试过显式等待,但是实际上我无法将其集成到我的特定需求中,因为我最近开始学习编程
import time
refresh_time_in_seconds = 3
driver.get("URL")
url = driver.current_url
while(True):
if url == driver.current_url:
driver.refresh()
url = driver.current_url
time.sleep(refresh_time_in_seconds)
上面的代码对我来说很好用,但刷新时无法集成元素搜索。如果任务在下一页刷新后出现,我需要选择
答案 0 :(得分:0)
我只是添加else条件,在该条件下我们可以获取task元素,然后中断循环,以便在退出循环时以及当url包含信息时您都可以准备好元素。
import time
refresh_time_in_seconds = 3
driver.get("URL")
url = driver.current_url
while(True):
if url == driver.current_url:
driver.refresh()
else: # <== this condition is to get the element and exit the while loop
# get the element here
task_ele = driver.find_element_by_xpath("xpath goes here")
break
url = driver.current_url
time.sleep(refresh_time_in_seconds)
# now you can perform operations on the task
task_ele.click()