0

我需要运行这个循环,直到网页上出现“.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
4

1 回答 1

-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'):
   ...
   ...
   ...
于 2016-09-23T15:57:33.043 回答