2017-08-16 56 views
1

我做一個基礎的測試摩卡。 節點v8.2.1硒的webdriver:^ 3.5.0+的NodeJS硒司機NoSuchElementError&StaleElementReferenceError

test.it('demoClass',() => { 
    driver.classes[0].findElement(By.css('.class-avatar')).click(); 
    driver.wait(until.elementIsVisible(driver.findElement(By.css('.anticon.anticon-plus')))); 
    //driver.sleep(2000); 
    driver.findElement(By.css('.anticon.anticon-plus')).click(); 
}) 

我得到兩種不同類型的錯誤,無論是其NoSuchElementError: no such element: Unable to locate element:StaleElementReferenceError: stale element reference: element is not attached to the page document

但無論誤差,其參考線:

driver.findElement(By.css(」。 。anticon.anticon加))點擊();

當我使用driver.sleep(2000),它得到解決。在我看來,這是動畫的問題。我只能在當時獲得元素(.anticon.ancicon-plus),頁面的動畫完成。

我很困惑的是,我使用driver.wait(until.elementIsVisible())沒有一個錯誤,很明顯,我的元素。但在下一行,我無法使用它。或者NoSuchElementErrorStaleElementReferenceError

我找到像http://www.seleniumhq.org/exceptions/stale_element_reference.jsp,https://stackoverflow.com/questions/18225997/stale-element-reference-element-is-not-attached-to-the-page-document一些答案。但它不能幫助我。

回答

-1

時使用driver.findElement,什麼可怕的事情將被觸發。使用JavaScript代替它。

driver.executeScript(function() { 
    while(true) { 
     if(!document.querySelector('.anticon.anticon-plus')){} 
     else { 
      document.querySelector('.anticon.anticon-plus').click(); 
      break; 
     } 
    } 
    return true; // not neccessary 
}) 
+0

這個答案和代碼沒有意義。 – JeffC

+0

它真的幫助我。我不知道selenium-webdriver是做什麼的,但是這個方法是由javascript生效的。我運行它沒有可怕的錯誤。 [定影薄片](https://github.com/tastejs/todomvc/pull/1371),使用像角的幀時,反應...,這可能是HAVA當你做某件事,或者當一個重新渲染頁面已加載。那時,硒-webdriver會給我們一個錯誤。但是,JavaScript仍然有用。 @JeffC – yuwanlin

+0

謝謝,它實際上解決了這個問題。@ yuwanlin – real