当前位置: 首页>>代码示例>>Java>>正文


Java WebDriver.findElement方法代码示例

本文整理汇总了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);
	}
}
 
开发者ID:LinuxSuRen,项目名称:phoenix.webui.framework,代码行数:26,代码来源:PrioritySearchStrategy.java

示例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;
    }
 
开发者ID:AshokKumarMelarkot,项目名称:msa-cucumber-appium,代码行数:20,代码来源:Locator.java

示例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());
		}
	}
}
 
开发者ID:xtf-cz,项目名称:xtf,代码行数:17,代码来源:DriverUtil.java

示例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();
}
 
开发者ID:CieloSun,项目名称:FashionSpider,代码行数:10,代码来源:Test.java

示例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"));
}
 
开发者ID:willhaben,项目名称:willtest,代码行数:8,代码来源:FirstExample.java

示例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);
    }
}
 
开发者ID:Axway,项目名称:ats-framework,代码行数:47,代码来源:HtmlFileBrowse.java

示例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();
}
 
开发者ID:getgauge-examples,项目名称:java-maven-selenium,代码行数:12,代码来源:CustomerSignup.java

示例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();			
}
 
开发者ID:Arquisoft,项目名称:participationSystem3b,代码行数:12,代码来源:SeleniumUtils.java

示例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"));
}
 
开发者ID:willhaben,项目名称:willtest,代码行数:11,代码来源:EventListenerExample.java

示例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();
    }
}
 
开发者ID:nribeka,项目名称:twitter-stuff,代码行数:48,代码来源:StatusUtils.java

示例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"));
}
 
开发者ID:willhaben,项目名称:willtest,代码行数:8,代码来源:FirstExample.java

示例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;
	}
}
 
开发者ID:xtf-cz,项目名称:xtf,代码行数:14,代码来源:DriverUtil.java

示例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();
}
 
开发者ID:mluukkai,项目名称:ohjelmistotuotanto2017,代码行数:26,代码来源:Tester.java

示例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();
}
 
开发者ID:xbynet,项目名称:crawler,代码行数:11,代码来源:WindowUtil.java

示例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());
}
 
开发者ID:getgauge-examples,项目名称:java-maven-selenium,代码行数:7,代码来源:LogIn.java


注:本文中的org.openqa.selenium.WebDriver.findElement方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。