How to judge that the element is clickable or not? – Selenium

enter image description here

For the URL I’m trying to induce clicks on next page button. To make sure that the button is clickable or not I try the following :

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.fa.fa-angle-right')))

But this gives true for even the last page. Where am I going wrong in my understanding? How can I judge that a button is clickable or not anymore? On the last page, the next page button is grayed out.

Answer

If you pay attention, when the button is getting grayed out, it has a class attribute whose attribute value is getting changed to disabled.

You can probably make a function and put the below code inside that, can call that as per your requirement.

Sample code :

wait = WebDriverWait(driver, 20)
next_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@aria-label='next page']")))
try:
    if next_button.get_attribute('class') == "disabled":
        print('Next button is not clickable anymore')
    else:
        print('Next button is available')
        next_button.click()
except:
    print('Something went wrong')
    pass

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC