Java Code Examples for org.openqa.selenium.SearchContext#findElements()

The following examples show how to use org.openqa.selenium.SearchContext#findElements() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractElementSearchAction.java    From vividus with Apache License 2.0 6 votes vote down vote up
private List<WebElement> findElements(SearchContext searchContext, By locator, SearchParameters parameters,
        boolean retry)
{
    List<WebElement> elements = parameters.isWaitForElement()
            ? waitForElement(searchContext, locator)
            : searchContext.findElements(locator);
    boolean elementsFound = null != elements;
    LOGGER.info("Total number of elements found {} is equal to {}", locator, elementsFound ? elements.size() : 0);
    if (elementsFound)
    {
        Visibility visibility = parameters.getVisibility();
        try
        {
            return Visibility.ALL == visibility
                    ? elements
                    : filterElementsByVisibility(elements, visibility == Visibility.VISIBLE, retry);
        }
        catch (StaleElementReferenceException e)
        {
            return findElements(searchContext, locator, parameters, true);
        }
    }
    return List.of();
}
 
Example 2
Source File: TeasyExpectedConditions.java    From teasy with MIT License 6 votes vote down vote up
private static WebElement getFirstVisibleWebElement(SearchContext searchContext, By locator) {
    try {
        final WebElement foundElement = searchContext.findElement(locator);
        if (isAvailable(foundElement)) {
            return foundElement;
        } else {
            List<WebElement> elements = searchContext.findElements(locator);
            for (WebElement element : elements) {
                if (isAvailable(element)) {
                    return element;
                }
            }
            return null;
        }
    } catch (Exception e) {
        return null;
    }
}
 
Example 3
Source File: AbstractSeleniumAttrLocator.java    From phoenix.webui.framework with Apache License 2.0 6 votes vote down vote up
@Override
public WebElement findElement(SearchContext driver)
{
	String attrName = getAttrName();
	String attrVal = getValue();
	By by = getBy();

	List<WebElement> elementList = driver.findElements(by);
	for(WebElement ele : elementList)
	{
		if(driver instanceof WebDriver)
		{
			new Actions((WebDriver) driver).moveToElement(ele);
		}
		
		if(attrVal.equals(ele.getAttribute(attrName)))
		{
			return ele;
		}
	}
	
	return null;
}
 
Example 4
Source File: TeasyExpectedConditions.java    From teasy with MIT License 5 votes vote down vote up
public static ExpectedCondition<WebElement> presenceOfElementLocatedBy(final SearchContext searchContext, final By locator) {
    return new ExpectedCondition<WebElement>() {
        @Override
        public WebElement apply(final WebDriver driver) {
            List<WebElement> els = searchContext.findElements(locator);
            return els.isEmpty() ? null : els.get(0);
        }

        @Override
        public String toString() {
            return String.format("presence of element located by %s -> %s", searchContext, locator);
        }
    };
}
 
Example 5
Source File: AbstractLocator.java    From phoenix.webui.framework with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public List<E> findElements(SearchContext driver)
{
	By by = getBy();
	
	if(driver instanceof WebDriver)
	{
		elementWait((WebDriver) driver, getTimeout(), by);
	}
	
	return (List<E>) driver.findElements(by);
}
 
Example 6
Source File: ResultsListWidgetITCase.java    From find with MIT License 5 votes vote down vote up
private void checkOutOfViewClass(final SearchContext widget) {
    final List<WebElement> results = widget.findElements(By.cssSelector(".search-result"));

    results.forEach(result -> {
        final boolean isDisplayed = result.isDisplayed();
        final boolean hasClass = result.getAttribute("class").contains("out-of-view");

        assertTrue(
                "All invisible results (and only invisible results) must have the out-of-view class",
                isDisplayed ^ hasClass
        );
    });
}
 
Example 7
Source File: BestMatchBy.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
/**
 * Returns 'best' result from by.
 * If there is no result: returns null,
 * if there is just one that is best,
 * otherwise the 'bestFunction' is applied to all results to determine best.
 * @param by by to use to find elements.
 * @param context context to search in.
 * @param <T> type of element expected.
 * @return 'best' element, will be <code>null</code> if no elements were found.
 */
public static <T extends WebElement> T findElement(By by, SearchContext context) {
    WebElement element = null;
    List<WebElement> elements = context.findElements(by);
    if (elements.size() == 1) {
        element = elements.get(0);
    } else if (elements.size() > 1) {
        element = BEST_FUNCTION.apply(context, elements);
    }
    return (T) element;
}
 
Example 8
Source File: ByAll.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Override
public WebElement findElement(SearchContext context) {
  for (By by : bys) {
    List<WebElement> elements = context.findElements(by);
    if (!elements.isEmpty()) {
      return elements.get(0);
    }
  }
  throw new NoSuchElementException("Cannot locate an element using " + toString());
}
 
Example 9
Source File: ByCssFinder.java    From webtau with Apache License 2.0 4 votes vote down vote up
@Override
public List<WebElement> find(SearchContext parent) {
    return parent.findElements(By.cssSelector(css));
}
 
Example 10
Source File: SeleniumCssLocator.java    From phoenix.webui.framework with Apache License 2.0 4 votes vote down vote up
@Override
public WebElement findElement(SearchContext driver)
{
	final String priorityCss; //优先定位的css值
	final String targetCss;
	final String css = getValue();
	
	String[] priorityCssArray = css.split(",");
	if(priorityCssArray.length >= 2)
	{
		targetCss = priorityCssArray[1];
		priorityCss = priorityCssArray[0];
	}
	else
	{
		targetCss = css;
		priorityCss = css.split(" ")[0];
	}
	
	//设定当前的定位器
	setValue(priorityCss);
	By by = getBy();
	
	//值还原
	setValue(css);
	List<WebElement> elementList = driver.findElements(by);
	for(WebElement ele : elementList)
	{
		if(driver instanceof WebDriver)
		{
			new Actions((WebDriver) driver).moveToElement(ele);
		}
		
		if(targetCss.equals(ele.getAttribute("class")))
		{
			return ele;
		}
	}
	
	return null;
}
 
Example 11
Source File: LazyLoadingPageObjectList.java    From webtester-core with Apache License 2.0 4 votes vote down vote up
private List<WebElement> findElementsForList() {
    SearchContext searchContext = model.getSearchContext();
    return searchContext.findElements(model.getSeleniumBy());
}
 
Example 12
Source File: ContentMappedBy.java    From java-client with Apache License 2.0 4 votes vote down vote up
@Override public List<WebElement> findElements(SearchContext context) {
    return context.findElements(map.get(currentContent));
}