簡體   English   中英

如何在Selenium python中單擊直到下一頁不可交互?

[英]How to click until next page is noninteractable in Selenium python?

當滾動“下一頁”按鈕的某個頁面時,我想一直點擊直到顯示所有內容。 然而,這個網站的問題是最后按鈕並沒有消失,它可以被找到,但處於不可交互狀態,如果點擊它,頁面將返回錯誤。 我應該如何在異常之前停止點擊? 下面的代碼將使頁面返回錯誤,因為它會點擊最后一個“不可交互”按鈕。

next_button=True
while next_button:
    try:
        next_button = driver.find_element_by_class_name('next_page')
        next_button.click()
    except ElementNotInteractableException:
        next_button=False
        break 

您可以像這樣創建用戶定義的函數:

def elementPresent(locatorType, locator):
#present = true
#not present = false
wait = WebDriverWait(driver, 20)
try:
    wait.until(EC.presence_of_element_located((locatorType, locator)))
    wait.until(EC.visibility_of_element_located((locatorType, locator)))
except Exception:
    return False
return True

這個功能基本上是檢查元素的存在和可見性。 如果找到具有給定定位器的元素,則函數返回 True 否則返回 False

要調用該函數,您可以使用循環,例如 while 循環

例如:

while(locatorType, locator):
    element = driver.find_element_by_class_name('next_page')
    element.click()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM