yandex-qatools / htmlelements

Html Elements is a Java framework providing easy-to-use way of interaction with web-page elements in web-page tests.
Other
270 stars 116 forks source link

Best price to avoid StaleElementReferenceException when dealing with ajax #81

Closed akrambek closed 9 years ago

akrambek commented 9 years ago

Hi all,

I was not sure If it is right place to ask this question but since I start to use this framework. I though you can help me out :). Basically, I am facing an issue where by I am trying to create a block Bootstrap Select2 element with ajax search. When I am searching I am getting StaleElementReferenceException most of the time because of issue described http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp.

So I would like to know how do you solve this issue. I thought you may have came across this issue since it is common functionality. Any help appreciated!

Here is snipped of code I have

public void selectByValue(String value){
        selectLink.click();
        searchTextInput.clear();
        searchTextInput.sendKeys(value);

        List<WebElement> options = getWrappedElement().findElements(By.xpath("//*[@id='select2-drop']/ul/li/div/div"));

        if(options.size() == 0){
            throw new NoSuchElementException("Cannot locate option with name: " + value);
        }

        for (WebElement option : options) {
            if (option.getText().equals(value)) {
                option.click();
                break;
            }
        }
    }
artkoshelev commented 9 years ago

please ask your questions on stackoverflow with #htmlelements tag