2017-08-12 100 views
0

我試圖使用expected_conditions.element_to_be_clickable,但它似乎沒有工作。在大約30%的運行中,我仍然看到「元素...在點上不可點擊」的錯誤。selenium:等待元素可點擊不起作用

以下是完整的錯誤消息:

selenium.common.exceptions.WebDriverException: Message: unknown error: Element ... is not clickable at point (621, 337). Other element would receive the click: ... (Session info: chrome=60.0.3112.90) (Driver info: chromedriver=2.26.436421 (6c1a3ab469ad86fd49c8d97ede4a6b96a49ca5f6),platform=Mac OS X 10.12.6 x86_64)

這裏是我的工作代碼:

def wait_for_element_to_be_clickable(selector, timeout=10): 
    global driver 
    wd_wait = WebDriverWait(driver, timeout) 

    wd_wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector)), 
    'waiting for element to be clickable ' + selector) 
    print ('WAITING') 
    return driver.find_element_by_css_selector(selector) 

更新:

所以現在這是非常奇怪的。即使我添加了幾個固定的等待時間,它仍然偶爾會拋出錯誤消息。以下是撥打電話的代碼:

sleep(5) 
    elem = utils.wait_for_element_to_be_clickable('button.ant-btn-primary') 
    sleep(5) 
    elem.click() 

回答

0

問題在錯誤消息中說明。

Element ... is not clickable at point (621, 337). Other element would receive the click: ...

問題是某些元素(您從錯誤消息中刪除的細節)位於您嘗試點擊的元素的頂部。在很多情況下,這是一些對話或其他一些UI元素。如何處理這取決於情況。如果它是一個打開的對話框,關閉它。如果它是一個關閉的對話框,但代碼運行速度很快,請等待對話框的某個UI元素不可見(對話框關閉,不再可見),然後嘗試點擊。通常它只是讀取阻塞的元素的HTML,在DOM中找到它,並找出如何等待它消失等。

+1

有人會認爲該函數在進行isClickable測定時會考慮到這一點。我最終創建了自己的函數來捕獲異常並執行重試。 – opike

0

結束創建我自己的自定義函數來處理異常並執行重試次數:

""" custom clickable wait function that relies on exceptions. """ 
def custom_wait_clickable_and_click(selector, attempts=5): 
    count = 0 
    while count < attempts: 
    try: 
     wait(1) 
     # This will throw an exception if it times out, which is what we want. 
     # We only want to start trying to click it once we've confirmed that 
     # selenium thinks it's visible and clickable. 
     elem = wait_for_element_to_be_clickable(selector) 
     elem.click() 
     return elem 

    except WebDriverException as e: 
     if ('is not clickable at point' in str(e)): 
     print('Retrying clicking on button.') 
     count = count + 1 
     else: 
     raise e 

    raise TimeoutException('custom_wait_clickable timed out') 
相關問題