本文整理汇总了Java中org.openqa.selenium.WebDriver.findElement方法的典型用法代码示例。如果您正苦于以下问题:Java WebDriver.findElement方法的具体用法?Java WebDriver.findElement怎么用?Java WebDriver.findElement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.openqa.selenium.WebDriver
的用法示例。
在下文中一共展示了WebDriver.findElement方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findElement
import org.openqa.selenium.WebDriver; //导入方法依赖的package包/类
/**
* 通用的元素查找方法
* @param by 具体的查找方法
* @return
*/
private WebElement findElement(By by)
{
WebDriver driver = engine.getDriver();
driver = engine.turnToRootDriver(driver);
if(element.getTimeOut() > 0)
{
WebDriverWait wait = new WebDriverWait(driver, element.getTimeOut());
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
}
if(parentElement != null)
{
return parentElement.findElement(by);
}
else
{
return driver.findElement(by);
}
}
示例2: getWebElement
import org.openqa.selenium.WebDriver; //导入方法依赖的package包/类
@Override
public WebElement getWebElement(WebDriver driver) {
WebElement webElement = null;
// Class<By> cls = (Class<By>) Class.forName("org.openqa.selenium.By");
// Method m = cls.getMethod(this.typeOfLocator, String[].class);
// String[] params = {this.attributeValue};
if(this.typeOfLocator.equals("id")){
webElement = driver.findElement(By.id(this.attributeValue));
}else if(this.typeOfLocator.equals("css")){
webElement = driver.findElement(By.cssSelector(this.attributeValue));
}
// return driver.findElement((By) m.invoke(null, (Object) params));
return webElement;
}
示例3: waitFor
import org.openqa.selenium.WebDriver; //导入方法依赖的package包/类
public static void waitFor(WebDriver driver, long timeout, By... elements) throws TimeoutException, InterruptedException {
try {
WaitUtil.waitFor(() -> elementsPresent(driver, elements), null, 1000L, timeout);
} catch (TimeoutException ex) {
try {
for (By element : elements) {
WebElement webElement = driver.findElement(element);
if (!webElement.isDisplayed()) {
throw new TimeoutException("Timeout exception during waiting for web element: " + webElement.getText());
}
}
} catch (NoSuchElementException | StaleElementReferenceException x) {
throw new TimeoutException("Timeout exception during waiting for web element: " + x.getMessage());
}
}
}
示例4: testSelenium
import org.openqa.selenium.WebDriver; //导入方法依赖的package包/类
public static void testSelenium() throws Exception {
System.getProperties().setProperty("webdriver.chrome.driver", "chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
webDriver.get("http://huaban.com/");
Thread.sleep(5000);
WebElement webElement = webDriver.findElement(By.xpath("/html"));
System.out.println(webElement.getAttribute("outerHTML"));
webDriver.close();
}
示例5: openPage
import org.openqa.selenium.WebDriver; //导入方法依赖的package包/类
@Test
public void openPage() {
WebDriver webDriver = seleniumRule.getWebDriver();
webDriver.get(WILLTEST_GITHUB_PAGE);
WebElement element = webDriver.findElement(By.cssSelector(REPO_HEADER_LOCATOR));
assertThat(element.getText(),is("willhaben/willtest"));
}
示例6: setFileInputValue
import org.openqa.selenium.WebDriver; //导入方法依赖的package包/类
/**
*
* @param webDriver {@link WebDriver} instance
* @param value the file input value to set
*/
protected void setFileInputValue( WebDriver webDriver, String value ) {
String locator = this.getElementProperties()
.getInternalProperty(HtmlElementLocatorBuilder.PROPERTY_ELEMENT_LOCATOR);
String css = this.getElementProperty("_css");
WebElement element = null;
if (!StringUtils.isNullOrEmpty(css)) {
element = webDriver.findElement(By.cssSelector(css));
} else {
element = webDriver.findElement(By.xpath(locator));
}
try {
element.sendKeys(value);
} catch (ElementNotVisibleException enve) {
if (!UiEngineConfigurator.getInstance().isWorkWithInvisibleElements()) {
throw enve;
}
// try to make the element visible overriding some CSS properties
// but keep in mind that it can be still invisible using another CSS and/or JavaScript techniques
String styleAttrValue = element.getAttribute("style");
JavascriptExecutor jsExec = (JavascriptExecutor) webDriver;
try {
jsExec.executeScript("arguments[0].setAttribute('style', arguments[1]);",
element,
"display:'block'; visibility:'visible'; top:'auto'; left:'auto'; z-index:999;"
+ "height:'auto'; width:'auto';");
element.sendKeys(value);
} finally {
jsExec.executeScript("arguments[0].setAttribute('style', arguments[1]);", element,
styleAttrValue);
}
} catch (InvalidElementStateException e) {
throw new SeleniumOperationException(e.getMessage(), e);
}
}
示例7: registerCustomerWith
import org.openqa.selenium.WebDriver; //导入方法依赖的package包/类
@Step("Sign up as <customer> with email <[email protected]> and <password>")
public void registerCustomerWith(String customer, String email, String password) {
WebDriver webDriver = Driver.webDriver;
webDriver.findElement(By.linkText("Sign up")).click();
WebElement form = webDriver.findElement(By.id("new_user"));
form.findElement(By.name("user[username]")).sendKeys(customer);
form.findElement(By.name("user[email]")).sendKeys(email);
form.findElement(By.name("user[password]")).sendKeys(password);
form.findElement(By.name("user[password_confirmation]")).sendKeys(password);
form.findElement(By.name("commit")).click();
}
示例8: ClickSubopcionMenuHover
import org.openqa.selenium.WebDriver; //导入方法依赖的package包/类
static public void ClickSubopcionMenuHover(WebDriver driver, String submenu, String opcionclick)
{
//Pasamos el raton por el submenu de Gestion de alumnos para
//que aparezca el menu desplegable
Actions builder = new Actions(driver);
WebElement hoverElement = driver.findElement(By.id(submenu));
builder.moveToElement(hoverElement).perform();
//Pinchamos la opcion opcionclick
By locator = By.id(opcionclick);
driver.findElement(locator).click();
}
示例9: testEventListenerWithError
import org.openqa.selenium.WebDriver; //导入方法依赖的package包/类
@Test
public void testEventListenerWithError() {
WebDriver webDriver = rule.getWebDriver();
webDriver.get("https://github.com");
WebElement searchInput = webDriver.findElement(By.cssSelector(".header-search-input"));
searchInput.sendKeys("will", "test");
searchInput.sendKeys(Keys.ENTER);
String searchKeyword = webDriver.findElement(By.cssSelector(".header-search-input")).getAttribute("value");
assertThat(searchKeyword, is("foooooo"));
}
示例10: takeStatusScreenshot
import org.openqa.selenium.WebDriver; //导入方法依赖的package包/类
public static void takeStatusScreenshot(Status status, MongoDatabase mongoDatabase, WebDriver driver) {
long id = status.getId();
User user = status.getUser();
try {
// visit the twitter url
driver.get(TWITTER_BASE_URL + user.getScreenName() + "/status/" + id);
WebDriverWait webDriverWait = new WebDriverWait(driver, 4);
webDriverWait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(PERMALINK_TWEET_CONTAINER)));
// Find the tweet container
WebElement element = driver.findElement(By.cssSelector(PERMALINK_TWEET_CONTAINER));
// take a full screenshot
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
// create in memory image data
BufferedImage screenshotImage = ImageIO.read(screenshotFile);
// calculate the position of the tweet element
Point point = element.getLocation();
int elementWidth = element.getSize().getWidth();
int elementHeight = element.getSize().getHeight();
// crop to get only the container of the tweet itself (sometimes this doesn't work on mac)
BufferedImage elementScreenshotImage =
screenshotImage.getSubimage(
point.getX() + 4,
point.getY() + 4,
elementWidth - 4,
screenshotImage.getHeight() < point.getY() + elementHeight
? screenshotImage.getHeight() - 4
: elementHeight - 4);
ImageIO.write(elementScreenshotImage, "png", screenshotFile);
FileUtils.copyFile(screenshotFile, new File("screenshot/" + id + ".png"));
} catch (Exception e) {
saveFailureStatusProcesssed(mongoDatabase, id, false);
System.out.println(
String.format(
"Unable to create screenshot for %s with id: %d",
user.getScreenName(),
id));
e.printStackTrace();
}
}
示例11: buggyTest
import org.openqa.selenium.WebDriver; //导入方法依赖的package包/类
@Test
public void buggyTest() {
WebDriver webDriver = seleniumRule.getWebDriver();
webDriver.get(WILLTEST_GITHUB_PAGE);
WebElement element = webDriver.findElement(By.cssSelector(REPO_HEADER_LOCATOR));
assertThat(element.getText(),is("fooooo"));
}
示例12: elementsPresent
import org.openqa.selenium.WebDriver; //导入方法依赖的package包/类
public static boolean elementsPresent(WebDriver driver, By... elements) {
try {
for (By element : elements) {
WebElement webElement = driver.findElement(element);
if (!webElement.isDisplayed()) {
return false;
}
}
return true;
} catch (NoSuchElementException | StaleElementReferenceException x) {
return false;
}
}
示例13: main
import org.openqa.selenium.WebDriver; //导入方法依赖的package包/类
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:4567");
sleep(2);
WebElement element = driver.findElement(By.linkText("login"));
element.click();
sleep(2);
element = driver.findElement(By.name("username"));
element.sendKeys("pekka");
element = driver.findElement(By.name("password"));
element.sendKeys("akkep");
element = driver.findElement(By.name("login"));
sleep(2);
element.submit();
sleep(3);
driver.quit();
}
示例14: clickElement
import org.openqa.selenium.WebDriver; //导入方法依赖的package包/类
/**
* 点击一个元素
* @param driver
* @param by
*/
public static void clickElement(WebDriver driver,By by){
WebElement tmp=driver.findElement(by);
Actions actions=new Actions(driver);
actions.moveToElement(tmp).click().perform();
}
示例15: giveAnOptionToLogIn
import org.openqa.selenium.WebDriver; //导入方法依赖的package包/类
@Step("Give an option to Log In")
public void giveAnOptionToLogIn() {
WebDriver webDriver = Driver.webDriver;
WebElement logOut = webDriver.findElement(By.linkText("Log in"));
assertTrue(logOut.isDisplayed());
}