运行 while 循环直到 css 选择器出现在网页上

run a while loop untill a css selector is present on web page

我需要运行这个循环直到“.loadMore”css选择器出现在网页上:

while ec.presence_of_element_located('.loadMore'):
        element_number = 25 * i
        wait = WebDriverWait(driver, time1);
        sub_button = (by, hook + str(element_number))
        wait.until(ec.presence_of_element_located(sub_button))
        driver.find_element_by_css_selector(button).click()
        time.sleep(5)  # Makes the page wait for the element to change
        i += 1

编写 def 以检查元素是否存在

from selenium.common.exceptions import NoSuchElementException        
def check_element_presence(selector):
    try:
        webdriver.find_element_by_css_selector(selector)
    except NoSuchElementException:
        return False
    return True

现在运行喜欢

while check_element_presence('.loadmore'):
   ...
   ...
   ...