Python 如何正确设置此while循环?
我正在我的一个应用程序中创建一个函数,它将通过每隔5秒重新加载页面来检查缩放会议是否存在。我在Python3中使用Selenium 以下是我现在所做的:Python 如何正确设置此while循环?,python,python-3.x,selenium,Python,Python 3.x,Selenium,我正在我的一个应用程序中创建一个函数,它将通过每隔5秒重新加载页面来检查缩放会议是否存在。我在Python3中使用Selenium 以下是我现在所做的: def CheckZoomMeeting(): # Attempts to locate the Zoom meeting. try: element_present = driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/div
def CheckZoomMeeting():
# Attempts to locate the Zoom meeting.
try:
element_present = driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/div/div[1]/div[1]/div/div/div/div[1]/div[2]') # ADD THE JOIN MEETING XPATH HERE
# If the element does not exist, it is False.
except NoSuchElementException:
element_present = False
# This is a loop that will keep reloading the page every 5 seconds, until element_present does not equal False.
while element_present == False:
time.sleep(5)
driver.refresh()
# Once it equals something other than False (meaning the Zoom meeting is there), it will just continue on.
else:
pass
虽然我现在无法实际测试这一点(因为目前实际上没有设置缩放会议),但据我所知,这将永远保持重新加载,因为结果存储在元素\u present
中
因此,我要做的是创建一个循环,如果元素不在那里,则每5秒重新加载一次页面,每次都要检查元素是否在那里,然后else:pass#继续执行其余的代码
如果element\u present=driver,这将很容易。通过xpath('/html/body/div[1]/div/div/div[2]/div/div/div[1]/div[1]/div/div/div/div[1]/div[2]”查找元素。
返回了一个True
或False
变量,但默认情况下它不返回。我该怎么做才能使这个循环真正工作
这可能是我应该想到的一个简单的解决方案,但我没有想到。将变量设置为
None
,然后循环,直到变量设置为值
def CheckZoomMeeting():
element_present = None
# Attempts to locate the Zoom meeting.
while not element_present: # until element found
try:
element_present = driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/div/div[1]/div[1]/div/div/div/div[1]/div[2]') # ADD THE JOIN MEETING XPATH HERE
# If the element does not exist, it is False.
except NoSuchElementException:
element_present = False
# This is a loop that will keep reloading the page every 5 seconds, until element_present does not equal False.
if not element_present:
time.sleep(5)
driver.refresh()
# Once it equals something other than False (meaning the Zoom meeting is there), it will just continue on.
正是我想要的。谢谢,下次我会知道的!