Check whether element is clickable in selenium

后端 未结 3 1418
孤独总比滥情好
孤独总比滥情好 2020-12-10 12:36

I\'m able to verify whether or not an element exists and whether or not it is displayed, but can\'t seem to find a way to see whether it is \'clickable\' (Not talking about

3条回答
  •  攒了一身酷
    2020-12-10 12:56

    You can wait for the element to be clickable:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Firefox()
    driver.get("http://somedomain")
    
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, "myDynamicElement"))
    )
    

    Or, you can follow the EAFP principle and catch an exception being raised by click():

    from selenium.common.exceptions import WebDriverException
    
    try:
        element.click()
    except WebDriverException:
        print "Element is not clickable"
    

提交回复
热议问题