Wait for element to be clickable using python and Selenium

前端 未结 3 1464
隐瞒了意图╮
隐瞒了意图╮ 2021-01-25 04:54

There are ways to wait for an object e.g. a button to be clickable in selenium python. I use time.sleep() and/or WebDriverWait...until, it works fine.<

3条回答
  •  终归单人心
    2021-01-25 05:31

    I come up with this:

    def myClick(by, desc):
        wait = WebDriverWait(dr, 10)
        by = by.upper()
        if by == 'XPATH':
            wait.until(EC.element_to_be_clickable((By.XPATH, desc))).click()
        if by == 'ID':
            wait.until(EC.element_to_be_clickable((By.ID, desc))).click()
        if by == 'LINK_TEXT':
            wait.until(EC.element_to_be_clickable((By.LINK_TEXT, desc))).click()
    

    with this function, the code:

    driver.find_element_by_link_text('Show Latest Permit').click()
    

    will be

    myClick('link_text', 'Show Latest Permit')
    

    instead.

    I have run a couple weeks with hundreds of elements to click, I have not seen the errors any longer.

提交回复
热议问题