Python Selenium how to wait before clicking on link

前端 未结 2 2030
小蘑菇
小蘑菇 2021-01-02 04:45

I just wonder, how to have the browser wait before clicking on a link? My goal is that I\'m scraping from a dynamic webpage, the content is dynamic but I manage to get the f

2条回答
  •  庸人自扰
    2021-01-02 05:19

    You need to use Selenium Waits.

    In particular, element_to_be_clickable expected condition is what fits better than others:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, "myDynamicElement"))
    )
    element.click()
    

    where driver is your webdriver instance, 10 is the amount of seconds to wait for an element. With this setup, selenium would try to locate an element every 500 milliseconds for 10 seconds in total. It would throw TimeoutException after 10 seconds left if element would not be found.

提交回复
热议问题