2014-10-01 64 views
2

我有一個webobject,當我點擊它,我得到:檢查Web對象是否位於Selenium中的其他Web對象的前面?

selenium.common.exceptions.WebDriverException: Message: u'unknown error: Element is not clickable at point (128, 605). Other element would receive the click: ...\n (Session info: chrome=37.0.2062.120)\n (Driver info: chromedriver=2.10.267518,platform=Linux 3.13.0-36-generic x86_64)'

的原因是,有在它的前面,即約餅乾消息的其他對象。那麼當我點擊它時,如何確保Web對象前面沒有任何東西?

例子:

from selenium import webdriver 

browser = webdriver.Chrome() 
browser.get("http://cookie-script.com/") 
browser.set_window_size(800, 800) 

# Click the Start button that is behind the 'Cookies' popup 
el=browser.find_elements_by_xpath("id('sp-feature')/div[1]/p[2]/a[1]")[0] 
el.click() 
+0

也許會添加一些JavaScript /邏輯,只爲您的硒測試激活,隱藏不必要的彈出窗口等? – michaelb 2014-10-01 14:17:31

+0

@michaelb以及我正在測試一個網站,那麼這將是一件壞事。 – Pithikos 2014-10-01 14:18:48

+2

那麼,如果有什麼東西在可點擊的對象前面,那麼真正的用戶也會有麻煩,所以這也是不正確的。也許在你的測試中,首先關閉cookie信息? – michaelb 2014-10-01 14:20:27

回答

1

我似乎簡單地處理兩個特定的異常已經解決了它的hackish的方式的時刻。

from selenium.common.exceptions import WebDriverException, StaleElementReferenceException 

def click_element(el): 
    try: 
     el.click() 
     return True 
    except WebDriverException as exception: 
     print("Could not click on element. Maybe something is in front of it?") 
    except StaleElementReferenceException as exception: 
     print("Reference to element is not valid anymore.") 
    return False 

您可以exception.msg

訪問異常的消息這似乎做的時刻。我不確定我是否遺漏了一些案件,但迄今爲止它已經爲我工作。好消息是,如果點擊沒有成功,你會得到一個False,這是非常有用的,因爲Selenium的click()不會返回任何內容。

相關問題