1

I am doing a test based mocha. node v8.2.1, selenium-webdriver: ^3.5.0.

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();
})

I am getting two different types of errors, either its NoSuchElementError: no such element: Unable to locate element: or StaleElementReferenceError: stale element reference: element is not attached to the page document

But whichever error, its refer to line:

driver.findElement(By.css('.anticon.anticon-plus')).click();

When I use driver.sleep(2000), its getting resolved. In my opinion, It's the question of animation. I can get the element(.anticon.ancicon-plus) only at the time, the page's animation is completed.

what I am confused is that I use driver.wait(until.elementIsVisible()) without an error, It's obvious that I got the element. but at the next line, I can't use it. Or NoSuchElementError, or StaleElementReferenceError.

I find some answer like 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. But It can't help me.

user1211
  • 1,507
  • 1
  • 18
  • 27
real
  • 11
  • 2

1 Answers1

-1

when use driver.findElement, something terrible will be triggered. use javascript instead it.

driver.executeScript(function() {
    while(true) {
        if(!document.querySelector('.anticon.anticon-plus')){}
        else {
            document.querySelector('.anticon.anticon-plus').click();
            break;
        }
    }
    return true; // not neccessary
})
yuwanlin
  • 65
  • 1
  • 1
  • 10
  • It really help me. I don't know what selenium-webdriver does, But this method by javascript is valid. I run it without the terrible errors. [Fixing the flake](https://github.com/tastejs/todomvc/pull/1371), when use a frame like angular, react...,It may be hava a re-render when you do something or when the page is loaded. at the time, selenium-webdriver will give us a error. however, the javascript is still useful. @JeffC – yuwanlin Aug 17 '17 at 01:02