我正在尝试一次浏览一个网站,点击元素next
,HTML代码为class="pg-next"
。但是,最终,我将要结束,并且不再有next
元素,此时我想停止循环。我有这样的事情:
pg_next_exists = True
while pg_next_exists:
#
# carry out necessary actions
#
if ...: # if the pg-next element can be found
pass
else:
pg_next_exists = False # at which point the while loop stops
如何检查该元素是否仍然存在?
答案 0 :(得分:0)
为什么不使用wait
尝试类似这样的内容
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Firefox()
browser.get('http://example.com/')
try:
wait = WebDriverWait(browser, 10)
search = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'next')))
search.click()
except:
# Process element not found here