2017-08-16 36 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