Java Code Examples for org.openqa.selenium.By#ByCssSelector

The following examples show how to use org.openqa.selenium.By#ByCssSelector . 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: ByType.java    From Selenium-Foundation with Apache License 2.0 6 votes vote down vote up
/**
 * Get the CSS locator string that reproduces the specified Selenium locator
 * 
 * @param locator Selenium locator
 * @return CSS locator string; 'null' if unconvertible
 */
@SuppressWarnings("squid:S1142")
public static String cssLocatorFor(final By locator) {
    
    String val = valueOf(locator);
    
    if (locator instanceof By.ByClassName) {
        return "." + val;
    } else if (locator instanceof By.ByCssSelector) {
        return val;
    } else if (locator instanceof By.ById) {
        return "#" + val;
    } else if (locator instanceof By.ByLinkText) {
        LOGGER.warn(UNSUPPORTED_FOR_CSS, "ByLinkText");
    } else if (locator instanceof By.ByName) {
        return "[name=" + val + "]";
    } else if (locator instanceof By.ByPartialLinkText) {
        LOGGER.warn(UNSUPPORTED_FOR_CSS, "ByPartialLinkText");
    } else if (locator instanceof By.ByTagName) {
        return val;
    } else if (locator instanceof By.ByXPath) {
        LOGGER.warn(UNSUPPORTED_FOR_CSS, "ByXPath");
    }
    
    return null;
}
 
Example 2
Source File: ByType.java    From Selenium-Foundation with Apache License 2.0 6 votes vote down vote up
/**
 * Get the XPath locator string that reproduces the specified Selenium locator
 * 
 * @param locator Selenium locator
 * @return XPath locator string; 'null' if unconvertible
 */
@SuppressWarnings("squid:S1142")
public static String xpathLocatorFor(final By locator) {
    
    String val = valueOf(locator);
    
    if (locator instanceof By.ByClassName) {
        return ".//*[contains(concat(' ',@class,' '),' " + val + " ')]";
    } else if (locator instanceof By.ByCssSelector) {
        LOGGER.warn(UNSUPPORTED_FOR_XPATH, "ByCssSelector");
    } else if (locator instanceof By.ById) {
        return ".//*[@id='" + val + "']";
    } else if (locator instanceof By.ByLinkText) {
        return ".//a[.='" + val + "']";
    } else if (locator instanceof By.ByName) {
        return ".//*[@name='" + val + "']";
    } else if (locator instanceof By.ByPartialLinkText) {
        return ".//a[text()[contains(.,'" + val + "')]]";
    } else if (locator instanceof By.ByTagName) {
        return ".//" + val;
    } else if (locator instanceof By.ByXPath) {
        return val;
    }
    
    return null;
}
 
Example 3
Source File: PageBase.java    From SeleniumCucumber with GNU General Public License v3.0 6 votes vote down vote up
private By getFindByAnno(FindBy anno){
	log.info(anno);
	switch (anno.how()) {
	
	case CLASS_NAME:
		return new By.ByClassName(anno.using());
	case CSS:
		return new By.ByCssSelector(anno.using());
	case ID:
		return new By.ById(anno.using());
	case LINK_TEXT:
		return new By.ByLinkText(anno.using());
	case NAME:
		return new By.ByName(anno.using());
	case PARTIAL_LINK_TEXT:
		return new By.ByPartialLinkText(anno.using());
	case XPATH:
		return new By.ByXPath(anno.using());
	default :
		throw new IllegalArgumentException("Locator not Found : " + anno.how() + " : " + anno.using());
	}
}
 
Example 4
Source File: FakeWebDriver.java    From webtau with Apache License 2.0 5 votes vote down vote up
@Override
public List<WebElement> findElements(By by) {
    if (by instanceof By.ByCssSelector) {
        String rendered = by.toString();
        int idxOfColon = rendered.indexOf(":");
        String css = rendered.substring(idxOfColon + 1).trim();
        return fakesByCss.containsKey(css) ? Collections.singletonList(fakesByCss.get(css)) : Collections.emptyList();
    }

    return Collections.emptyList();
}
 
Example 5
Source File: RobustElementWrapper.java    From Selenium-Foundation with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"squid:S3776", "squid:MethodCyclomaticComplexity"})
public RobustElementWrapper(
                final WebElement element, final WrapsContext context, final By locator, final int index) {
    
    // if specified element is already robust
    if (element instanceof RobustWebElement) {
        RobustElementWrapper wrapper = ((InterceptionAccessor) element).getInterceptor();
        this.acquiredAt = wrapper.acquiredAt;
        
        this.wrapped = wrapper.wrapped;
        this.context = wrapper.context;
        this.locator = wrapper.locator;
        this.index = wrapper.index;
    } else {
        Objects.requireNonNull(context, "[context] must be non-null");
        Objects.requireNonNull(locator, "[locator] must be non-null");
        if (index < OPTIONAL) {
            throw new IndexOutOfBoundsException("Specified index is invalid");
        }
        
        this.wrapped = element;
        this.context = context;
        this.locator = locator;
        this.index = index;
    }
    
    driver = WebDriverUtils.getDriver(this.context.getWrappedContext());
    
    findsByCssSelector = (driver instanceof FindsByCssSelector);
    findsByXPath = (driver instanceof FindsByXPath);
    
    if ((this.index == OPTIONAL) || (this.index > 0)) {
        if (findsByXPath && ( ! (this.locator instanceof By.ByCssSelector))) {
            selector = ByType.xpathLocatorFor(this.locator);
            if (this.index > 0) {
                selector += "[" + (this.index + 1) + "]";
            }
            strategy = Strategy.JS_XPATH;
            
            this.locator = By.xpath(this.selector);
        } else if (findsByCssSelector) {
            selector = ByType.cssLocatorFor(this.locator);
            if (selector != null) {
                strategy = Strategy.JS_CSS;
            }
        }
    }
    
    if (this.wrapped == null) {
        if (this.index == OPTIONAL) {
            acquireReference(this);
        } else {
            refreshReference(null);
        }
    } else if (acquiredAt == 0) {
        acquiredAt = System.currentTimeMillis();
    }
}