下面列出了org.openqa.selenium.By#cssSelector ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void completeFillIdentifierSteps(String identifier) {
By switchAccountButtonBy = By.cssSelector("div[aria-label='Switch account']");
By useAnotherAccountButtonBy = By.xpath("//div[contains(text(), 'Use another account')]");
if (isElementPresent(switchAccountButtonBy)) {
click(switchAccountButtonBy);
waitForLoginPanelAnimationToComplete();
}
if (isElementPresent(useAnotherAccountButtonBy)) {
click(useAnotherAccountButtonBy);
waitForLoginPanelAnimationToComplete();
}
fillTextBox(identifierTextBox, identifier);
}
@Test
public void automateJavaScriptDragAndDrop() {
driver.get("http://web.masteringselenium.com/jsDragAndDrop.html");
Actions advancedActions = new Actions(driver);
final By destroyableBoxes = By.cssSelector("ul > li > div");
WebElement obliterator = driver.findElement(By.id("obliterate"));
WebElement firstBox = driver.findElement(By.id("one"));
WebElement secondBox = driver.findElement(By.id("two"));
assertThat(driver.findElements(destroyableBoxes).size()).isEqualTo(5);
advancedActions.clickAndHold(firstBox)
.moveToElement(obliterator)
.release()
.perform();
assertThat(driver.findElements(destroyableBoxes).size()).isEqualTo(4);
advancedActions.dragAndDrop(secondBox, obliterator).perform();
assertThat(driver.findElements(destroyableBoxes).size()).isEqualTo(3);
}
/**
* A helper method to convert Hook to By
* @param hook The hook that specifies a web element
* @return The converted By
* @throws CandybeanException
*/
public static By getBy(Hook hook) throws CandybeanException {
switch (hook.hookStrategy) {
case CSS:
return By.cssSelector(hook.hookString);
case XPATH:
return By.xpath(hook.hookString);
case ID:
return By.id(hook.hookString);
case NAME:
return By.name(hook.hookString);
case LINK:
return By.linkText(hook.hookString);
case PLINK:
return By.partialLinkText(hook.hookString);
case CLASS:
return By.className(hook.hookString);
case TAG:
return By.tagName(hook.hookString);
default:
throw new CandybeanException("Strategy type not recognized.");
}
}
public By pickFrom(String method, String selector) {
if ("class name".equals(method)) {
return By.className(selector);
} else if ("css selector".equals(method)) {
return By.cssSelector(selector);
} else if ("id".equals(method)) {
return By.id(selector);
} else if ("link text".equals(method)) {
return By.linkText(selector);
} else if ("partial link text".equals(method)) {
return By.partialLinkText(selector);
} else if ("name".equals(method)) {
return By.name(selector);
} else if ("tag name".equals(method)) {
return By.tagName(selector);
} else if ("xpath".equals(method)) {
return By.xpath(selector);
} else {
throw new WebDriverException("Cannot find matching element locator to: " + method);
}
}
private By getBy(Identifier identifier) {
LOG.debug("Finding selenium Element : " + identifier.getLocator() + " by : " + identifier.getIdentifier());
if (identifier.getIdentifier().equalsIgnoreCase("id")) {
return By.id(identifier.getLocator());
} else if (identifier.getIdentifier().equalsIgnoreCase("name")) {
return By.name(identifier.getLocator());
} else if (identifier.getIdentifier().equalsIgnoreCase("class")) {
return By.className(identifier.getLocator());
} else if (identifier.getIdentifier().equalsIgnoreCase("css")) {
return By.cssSelector(identifier.getLocator());
} else if (identifier.getIdentifier().equalsIgnoreCase("xpath")) {
return By.xpath(identifier.getLocator());
} else if (identifier.getIdentifier().equalsIgnoreCase("link")) {
return By.linkText(identifier.getLocator());
} else if (identifier.getIdentifier().equalsIgnoreCase("data-cerberus")) {
return By.xpath("//*[@data-cerberus='" + identifier.getLocator() + "']");
} else {
throw new NoSuchElementException(identifier.getIdentifier());
}
}
public QuestionPage answer(String description) {
WebElement answerForm = byClassName("answer-form");
byCSS("#wmd-input").sendKeys("");
By buttonSelector = By.cssSelector(".same-author-confirmation button");
if (isElementPresent(buttonSelector, answerForm)) {
waitForClickableElement(buttonSelector, 5);
answerForm.findElement(buttonSelector).click();
}
byCSS("#wmd-input").sendKeys(description);
answerForm.submit();
return new QuestionPage(driver);
}
private void zoomIntoHelsinore() throws InterruptedException {
if (browser instanceof JavascriptExecutor) {
((JavascriptExecutor) browser).executeScript("mapModule.map.setCenter(new OpenLayers.LonLat(12.65,56.035).transform(new OpenLayers.Projection(\"EPSG:4326\"), new OpenLayers.Projection(\"EPSG:900913\")), 12)");
}
browser.findElement(By.id("tab-map")).click();
final String expectedViewPortInfo = "(56°05'06.8\"N, 12°30'02.4\"E)\n(55°59'05\"N, 12°47'57.6\"E)";
By actualViewPortInfoElement = By.cssSelector("div#viewport.useroutput p");
wait.until(ExpectedConditions.textToBePresentInElement(actualViewPortInfoElement, expectedViewPortInfo));
}
/**
* selects the first picture with the label <b>orderLabel</b> in the selectbox
*
* @param orderLabel
*/
public void selectPictureWithOrder(String orderLabel) {
By selector = By.cssSelector(SELECTBOX_SELECTOR);
WebElement element = this.getDriver().findElement(selector);
Select select = new Select(element);
select.selectByVisibleText(orderLabel);
}
@Override
public void executeAfterJSPreconditionHasBeenSatisfied(WebDriver driver, ReplayingState replayingState, Function<SearchContext, SearchContext> contextProvider) {
String dataId = this.dataId.startsWith("{{")
? replayingState.getValue(this.dataId)
: this.dataId;
By selector = By.cssSelector("input[data-id='" + dataId + "'");
contextProvider.apply(driver).findElement(selector).click();
}
private By createBy(String locator) {
if (locator.startsWith("id=")) {
return By.id(StringUtils.remove(locator, "id="));
} else if (locator.startsWith("name=")) {
return By.name(StringUtils.remove(locator, "name="));
} else if (locator.startsWith("xpath=")) {
return By.xpath(StringUtils.remove(locator, "xpath="));
} else if (locator.startsWith("linkText=")) {
return By.linkText(StringUtils.remove(locator, "linkText="));
} else if (locator.startsWith("partialLinkText=")) {
return By.partialLinkText(StringUtils.remove(locator, "partialLinkText="));
} else if (locator.startsWith("cssSelector=")) {
return By.cssSelector(StringUtils.remove(locator, "cssSelector="));
} else if (locator.startsWith("css=")) {
return By.cssSelector(StringUtils.remove(locator, "css="));
} else if (locator.startsWith("tagName=")) {
return By.tagName(StringUtils.remove(locator, "tagName="));
} else if (locator.startsWith("className=")) {
return By.className(StringUtils.remove(locator, "className="));
} else if (locator.startsWith("By.id: ")) {
return By.id(StringUtils.remove(locator, "By.id: "));
} else if (locator.startsWith("By.name: ")) {
return By.name(StringUtils.remove(locator, "By.name: "));
} else if (locator.startsWith("By.xpath: ")) {
return By.xpath(StringUtils.remove(locator, "By.xpath: "));
} else if (locator.startsWith("By.linkText: ")) {
return By.linkText(StringUtils.remove(locator, "By.linkText: "));
} else if (locator.startsWith("By.partialLinkText: ")) {
return By.partialLinkText(StringUtils.remove(locator, "By.partialLinkText: "));
} else if (locator.startsWith("By.css: ")) {
return By.cssSelector(StringUtils.remove(locator, "By.css: "));
} else if (locator.startsWith("By.cssSelector: ")) {
return By.cssSelector(StringUtils.remove(locator, "By.cssSelector: "));
} else if (locator.startsWith("By.className: ")) {
return By.className(StringUtils.remove(locator, "By.className: "));
} else if (locator.startsWith("By.tagName: ")) {
return By.tagName(StringUtils.remove(locator, "By.tagName: "));
}
throw new RuntimeException(String.format("Unable to generate By using locator: '%s'!", locator));
}
protected By readLocatorArgument(String argName) {
Object argumentValue = readArgument(argName);
if (argumentValue instanceof String) {
Map<String, Object> newArgValue = new HashMap<String, Object>();
newArgValue.put("xpath", argumentValue);
argumentValue = newArgValue;
}
Map<String, Object> argValueAsMap = (Map<String, Object>) argumentValue;
if (argValueAsMap.containsKey("id")) {
return By.id(argValueAsMap.get("id").toString());
} else if (argValueAsMap.containsKey("accessibilityId")) {
return MobileBy.AccessibilityId(argValueAsMap.get("accessibilityId").toString());
} else if (argValueAsMap.containsKey("predicate")) {
return MobileBy.iOSNsPredicateString(argValueAsMap.get("predicate").toString());
} else if (argValueAsMap.containsKey("iosClassChain")) {
return MobileBy.iOSClassChain(argValueAsMap.get("iosClassChain").toString());
} else if (argValueAsMap.containsKey("androidUiAutomator")) {
return MobileBy.AndroidUIAutomator(argValueAsMap.get("androidUiAutomator").toString());
} else if (argValueAsMap.containsKey("name")) {
return By.name(argValueAsMap.get("name").toString());
} else if (argValueAsMap.containsKey("css")) {
return By.cssSelector(argValueAsMap.get("css").toString());
} else if (argValueAsMap.containsKey("class")) {
return By.className(argValueAsMap.get("class").toString());
} else if (argValueAsMap.containsKey("tag")) {
return By.tagName(argValueAsMap.get("tag").toString());
} else if (argValueAsMap.containsKey("linkText")) {
return By.linkText(argValueAsMap.get("linkText").toString());
} else if (argValueAsMap.containsKey("partialLinkText")) {
return By.partialLinkText(argValueAsMap.get("partialLinkText").toString());
} else if (argValueAsMap.containsKey("xpath")) {
String xpath = argValueAsMap.get("xpath").toString().replace("''", "'");
return By.xpath(xpath);
} else if (argValueAsMap.containsKey("image")) {
String imageBase64 = argValueAsMap.get("image").toString();
return MobileBy.image(imageBase64); // TO DO continue implementation
} else {
throw new RuntimeException(
"You must provide at least one valid identification method the locator "
+ "object by populating at least 1 of the following properties: id, name, "
+ "css, class, tag, linkText, partialLinkText, xpath.");
}
}
public static By getBy(String loc, PropertyUtil props) {
Gson gson = new Gson();
loc = props.getSubstitutor().replace(loc);
loc = props.getString(loc, loc);
JsonElement element = JSONUtil.getGsonElement(loc);
if ((null != element) && element.isJsonObject()) {
Object obj = gson.fromJson(element, Map.class).get("locator");
loc = obj instanceof String ? (String) obj :
gson.toJson(obj);
}
element = JSONUtil.getGsonElement(loc);
if ((null != element) && element.isJsonArray()) {
String[] locs = new Gson().fromJson(element, String[].class);
return new ByAny(locs);
}
if (loc.startsWith("//")) {
return By.xpath(loc);
} else if (loc.indexOf("=") > 0) {
String parts[] = loc.split("=", 2);
if (parts[0].equalsIgnoreCase("key") || parts[0].equalsIgnoreCase("property")) {
String val = props.getSubstitutor().replace(parts[1]);
return getBy(props.getString(val, val), props);
}
if (parts[0].equalsIgnoreCase("jquery")) {
return new ByJQuery(parts[1]);
}
if (parts[0].equalsIgnoreCase("extDom")) {
return new ByExtDomQuery(parts[1]);
}
if (parts[0].equalsIgnoreCase("extComp")) {
return new ByExtCompQuery(parts[1]);
}
if (parts[0].equalsIgnoreCase("name")) {
return By.name(parts[1]);
} else if (parts[0].equalsIgnoreCase("id")) {
return By.id(parts[1]);
} else if (parts[0].equalsIgnoreCase("xpath")) {
return By.xpath(parts[1]);
} else if (parts[0].equalsIgnoreCase("css")) {
return By.cssSelector(parts[1]);
} else if (parts[0].equalsIgnoreCase("link") || parts[0].equalsIgnoreCase("linkText")) {
return By.linkText(parts[1]);
} else if (parts[0].equalsIgnoreCase("partialLink") || parts[0].equalsIgnoreCase("partialLinkText")) {
return By.partialLinkText(parts[1]);
} else if (parts[0].equalsIgnoreCase("className")) {
return By.className(parts[1]);
} else if (parts[0].equalsIgnoreCase("tagName")) {
return By.tagName(parts[1]);
} else {
return new ByCustom(parts[0], parts[1]);
}
} else {
return By.xpath(String.format("//*[@name='%s' or @id='%s' or @value='%s']", loc, loc, loc));
}
}
@SProperty(name = "css")
public By getByCss(String css) {
return By.cssSelector(css);
}
public void waitForCopyErrorMessageToLoad() {
By errorMessageSelector = By.cssSelector("#question-copy-modal-status.alert-danger");
waitForElementPresence(errorMessageSelector);
waitForElementVisibility(browser.driver.findElement(errorMessageSelector));
}
public By getSelectorExecutor() {
return By.cssSelector(".executor-info-cell");
}
public boolean isCheckboxChecked(String checkboxClass, String checkboxValue, int questionNumber) {
By checkboxSelector = By.cssSelector("#questionTable-" + questionNumber + " input[value='" + checkboxValue
+ "']." + checkboxClass);
WebElement checkbox = browser.driver.findElement(checkboxSelector);
return checkbox.isSelected();
}
private By getPropertyField(Property property) {
return By.cssSelector(String.format("*[property=\"%s\"]", property.getName()));
}
/**
* Waits for all the panels to collapse.
*/
public void waitForPanelsToCollapse() {
By panelCollapseSelector = By.cssSelector("div[id^='panelBodyCollapse-']");
waitForElementsToDisappear(browser.driver.findElements(panelCollapseSelector));
}
@Override
public By createBy(String value) {
String escapedValue = CssSelectorUtils.escape(value);
return By.cssSelector(String.format(ID_STARTS_WITH_PATTERN, escapedValue));
}
@Override
public By createBy(String value) {
String escapedValue = CssSelectorUtils.escape(value);
return By.cssSelector(String.format(ID_ENDS_WITH_PATTERN, escapedValue));
}