org.openqa.selenium.By#cssSelector ( )源码实例Demo

下面列出了org.openqa.selenium.By#cssSelector ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: teammates   文件: GoogleLoginPage.java
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);
}
 
源代码3 项目: candybean   文件: Hook.java
/**
 * 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.");
	}
}
 
源代码4 项目: selenium   文件: BySelector.java
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);
  }
}
 
源代码5 项目: cerberus-source   文件: WebDriverService.java
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());
        }
    }
 
源代码6 项目: mamute   文件: QuestionPage.java
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);
}
 
源代码7 项目: AisAbnormal   文件: StatisticDataIT.java
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));
}
 
源代码8 项目: mycore   文件: ToolBarController.java
/**
 * 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);
}
 
源代码9 项目: bromium   文件: ClickDataId.java
@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();
}
 
源代码10 项目: carina   文件: LocalizedAnnotations.java
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));
}
 
源代码11 项目: opentest   文件: AppiumTestAction.java
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.");
    }
}
 
源代码12 项目: qaf   文件: LocatorUtil.java
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);
}
 
源代码14 项目: teammates   文件: InstructorFeedbackEditPage.java
public void waitForCopyErrorMessageToLoad() {
    By errorMessageSelector = By.cssSelector("#question-copy-modal-status.alert-danger");
    waitForElementPresence(errorMessageSelector);
    waitForElementVisibility(browser.driver.findElement(errorMessageSelector));
}
 
源代码15 项目: blueocean-plugin   文件: ExecutorPluginPage.java
public By getSelectorExecutor() {
    return By.cssSelector(".executor-info-cell");
}
 
源代码16 项目: teammates   文件: InstructorFeedbackEditPage.java
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();
}
 
源代码17 项目: elepy   文件: FillInBoolean.java
private By getPropertyField(Property property) {
    return By.cssSelector(String.format("*[property=\"%s\"]", property.getName()));
}
 
源代码18 项目: teammates   文件: InstructorFeedbackResultsPage.java
/**
 * Waits for all the panels to collapse.
 */
public void waitForPanelsToCollapse() {
    By panelCollapseSelector = By.cssSelector("div[id^='panelBodyCollapse-']");

    waitForElementsToDisappear(browser.driver.findElements(panelCollapseSelector));
}
 
源代码19 项目: webtester2-core   文件: IdStartsWith.java
@Override
public By createBy(String value) {
    String escapedValue = CssSelectorUtils.escape(value);
    return By.cssSelector(String.format(ID_STARTS_WITH_PATTERN, escapedValue));
}
 
源代码20 项目: webtester2-core   文件: IdEndsWith.java
@Override
public By createBy(String value) {
    String escapedValue = CssSelectorUtils.escape(value);
    return By.cssSelector(String.format(ID_ENDS_WITH_PATTERN, escapedValue));
}