繁体   English   中英

Python,Selenium:如何检查元素的存在?

[英]Python, Selenium: How to check existence of an element?

我试图一次浏览一个网站,单击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

如何检查该元素是否仍然存在?

你为什么不使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM