2014-10-01 65 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()不会返回任何内容。

相关问题