public WebElement findElement(SearchContext searchContext) { List<WebElement> elements = searchContext.findElements(by); if (next != null) { if (index > 0 ) { if (index <= elements.size()) { return next.findElement(elements.get(index - 1)); } } else { if (elements.size() > 0) { return next.findElement(elements.get(0)); } } } else { if (index > 0) { if (index <= elements.size()) { return elements.get(index - 1); } } else { if (elements.size() > 0) { return elements.get(0); } } } throw new NoSuchElementException(by.toString() + " | index " + index); }
@Override public Void execute(SelenideElement proxy, WebElementSource selectField, Object[] args) { String text = (String) args[0]; WebElement element = selectField.getWebElement(); Select select = new Select(element); List<WebElement> options = element.findElements(By.xpath( ".//option[contains(normalize-space(.), " + Quotes.escape(text) + ")]")); if (options.isEmpty()) { throw new NoSuchElementException("Cannot locate option containing text: " + text); } for (WebElement option : options) { setSelected(option); if (!select.isMultiple()) { break; } } return null; }
@Override public WebElement findElement(SearchContext context) { return bys.stream() .map(by -> getSearchingFunction(by).apply(context)) .filter(Optional::isPresent) .map(Optional::get) .findFirst() .orElseThrow(() -> new NoSuchElementException("Cannot locate an element using " + toString())); } }
private AndroidElement findElementByText(String text) { return driver.findElements(id("android:id/title")).stream().filter(androidElement -> text.equals(androidElement.getText())).findFirst() .orElseThrow(() -> new NoSuchElementException(String.format("There is no element with the text '%s'", text))); }
@Override public WebElement findElement(SearchContext context) { AppiumFunction<SearchContext, WebElement> searchingFunction = null; for (By by: bys) { searchingFunction = Optional.ofNullable(searchingFunction != null ? searchingFunction.andThen(getSearchingFunction(by)) : null).orElse(getSearchingFunction(by)); } FluentWait<SearchContext> waiting = new FluentWait<>(context); try { checkNotNull(searchingFunction); return waiting.until(searchingFunction); } catch (TimeoutException e) { throw new NoSuchElementException("Cannot locate an element using " + toString()); } } }
/** * Find the element. */ public WebElement findElement() { if (cachedElement != null && shouldCache) { return cachedElement; } By bySearching = getBy(this.by, searchContext); try { WebElement result = waitFor(() -> searchContext.findElement(bySearching)); if (shouldCache) { cachedElement = result; } return result; } catch (TimeoutException | StaleElementReferenceException e) { throw new NoSuchElementException(format(exceptionMessageIfElementNotFound, bySearching.toString()), e); } }
/** Scroll Timeout check * @param startTime - Long initial time for timeout count down **/ public static void checkTimeout(long startTime){ long elapsed = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())-startTime; if (elapsed > SCROLL_TIMEOUT) { throw new NoSuchElementException("Scroll timeout has been reached.."); } }
private void throwNoSuchElementExceptionWithCauseIfPresent(final Throwable timeout, final String defaultMessage) { String timeoutMessage = (timeout.getCause() != null) ? timeout.getCause().getMessage() : timeout.getMessage(); String finalMessage = (StringUtils.isNotEmpty(timeoutMessage)) ? timeoutMessage : defaultMessage; throw new NoSuchElementException(finalMessage, timeout); }
@Override public List<WebElement> findElementsByClassName(String className) { if (className.indexOf(' ') != -1) { throw new NoSuchElementException("Compound class names not permitted"); } return findElementsByCssSelector("." + className); }
/** * Check that all of the specified texts appears somewhere in the page. */ public void shouldContainAllText(final String... textValues) { if (!containsAllText(textValues)) { String errorMessage = String.format( "One of the text elements in '%s' was not found in the page", (Object[]) textValues); throw new NoSuchElementException(errorMessage); } }
@Override public WebElement findElementByClassName(String className) { if (className.indexOf(' ') != -1) { throw new NoSuchElementException("Compound class names not permitted"); } return findElementByCssSelector("." + className); }
@Override public WebElement findElement(SearchContext context) { return bys.stream() .map(by -> getSearchingFunction(by).apply(context)) .filter(Optional::isPresent) .map(Optional::get) .findFirst() .orElseThrow(() -> new NoSuchElementException("Cannot locate an element using " + toString())); } }
@Override public WebElement findElementByCssSelector(String using) { List<WebElement> allElements = parent.findElementsByCssSelector(using); allElements = findChildNodes(allElements); if (allElements.isEmpty()) { throw new NoSuchElementException("Cannot find child element using css: " + using); } return allElements.get(0); }
private List<WebElement> findMultipleElements(String using, String type) { Integer id = debugger.executeScriptOnObject(using, objectId); if (id == null) { throw new NoSuchElementException("Cannot find element(s) with " + type); } return parent.processElements(id); }
@SuppressWarnings("unchecked") @Override public List<WebElement> findElements(SearchContext context) { List<WebElement> elements = (List<WebElement>) ((JavascriptExecutor) context) .executeScript("var elements = $(arguments[0]).get(); return ((elements.length) ? elements : null)", jQuerySelector); if (elements != null){ return elements; } throw new NoSuchElementException("Cannot locate elements using " + toString()); }
@Override public WebElement findElement(SearchContext context) { WebElement element = (WebElement) ((JavascriptExecutor) context) .executeScript("return $(arguments[0]).get(0)", jQuerySelector); if (element != null){ return element; } throw new NoSuchElementException("Cannot locate element using " + toString()); }
private WebElement findElement(long timeout) { if (element != null) { return element; } if (isPresent(timeout)) { //TODO: investigate maybe searchContext better to use here! element = getDriver().findElement(by); } else { throw new NoSuchElementException("Unable to detect element using By: " + by.toString()); } return element; }
@Override public WebElement findElementById(String id) { if (!(lastPage() instanceof HtmlPage)) { throw new NoSuchElementException("Unable to locate element by id for " + lastPage()); } try { HtmlElement element = ((HtmlPage) lastPage()).getHtmlElementById(id); return newHtmlUnitWebElement(element); } catch (ElementNotFoundException e) { throw new NoSuchElementException("Unable to locate element with ID: " + id); } }
@Override public WebElement findElementByTagName(String name) { if (!(lastPage() instanceof HtmlPage)) { throw new IllegalStateException("Unable to locate element by name for " + lastPage()); } NodeList allElements = ((HtmlPage) lastPage()).getElementsByTagName(name); if (allElements.getLength() > 0) { return newHtmlUnitWebElement((HtmlElement) allElements.item(0)); } throw new NoSuchElementException("Unable to locate element with name: " + name); }
@Override public WebElement findElementByName(String name) { if (!(lastPage() instanceof HtmlPage)) { throw new IllegalStateException("Unable to locate element by name for " + lastPage()); } List<DomElement> allElements = ((HtmlPage) lastPage()).getElementsByName(name); if (!allElements.isEmpty()) { return newHtmlUnitWebElement(allElements.get(0)); } throw new NoSuchElementException("Unable to locate element with name: " + name); }