List of usage examples for org.openqa.selenium By tagName
public static By tagName(String tagName)
From source file:com.github.codewrs.selenium.FindElementAdvanced.java
License:Open Source License
/** * Find all <tr> tags within the current table body. * Then searches <th> tags for each <tr>. * Returns <tr> WebElement by matching with <th> text. * @param tBody WebElement of the table body. * @param thTextToMatch Text to match with <th> tag's text. * @param wait Explicit Wait Time./* w w w . j a v a 2 s. c o m*/ * @return WebElement or null if nothing matches. */ protected WebElement findElementOnTable(WebElement tBody, String thTextToMatch, WebDriverWait wait) { try { List<WebElement> trList = tBody.findElements(By.tagName("tr")); wait.until(ExpectedConditions.visibilityOfAllElements(trList)); int trSize = trList.size(); if (trSize > 0) { for (WebElement webElement : trList) { List<WebElement> tdList = webElement.findElements(By.tagName("td")); int thSize = tdList.size(); if (thSize > 0) { for (WebElement ele : tdList) { if (ele.getText() != null) { if (ele.getText().equals(thTextToMatch)) { System.out.println("The returned WebElement text : " + ele.getText()); return ele; } } } } } } } catch (NoSuchElementException e) { System.out.println("No Element found."); } System.out.println("No Element found."); return null; }
From source file:com.github.igorsuhorukov.webdriver.example.Project.java
public Project(String url, WebDriver driver) { logger.info(String.format("Fetching %s", url)); driver.get(url);//from w w w . jav a2 s.co m WebElement main = driver.findElement(By.id("main")); name = main.findElement(By.tagName("h3")).getText(); description = main.findElement(By.xpath("//h3/following-sibling::table/tbody/tr/td[1]")).getText(); link = main.findElement(By.xpath("//td[text()='HomePage']/following-sibling::*")).getText(); license = main.findElement(By.xpath("//td[text()='License']/following-sibling::*")).getText(); }
From source file:com.github.wiselenium.elements.component.TableTest.java
License:Open Source License
@Test public void shouldFindElementsInsideCells() { Table table = this.page.getComplexTableWithFoot(); TableBody tbody = table.getBody();//from www. j a v a 2 s. c o m TableRow firstRow = tbody.getRow(0); TableCell firstRowSecondCell = firstRow.getCell(1); Table innerTable = firstRowSecondCell.findElement(Table.class, By.tagName("table")); assertNotNull(innerTable); assertFalse(innerTable.getBody().getRows().isEmpty()); TableRow secondRow = tbody.getRow(1); TableCell secondRowFirstCell = secondRow.getCell(0); WebElement ul = secondRowFirstCell.findElement(WebElement.class, By.tagName("ul")); assertNotNull(ul); }
From source file:com.github.wiselenium.elements.component.TableTest.java
License:Open Source License
@Test public void shouldGetOnlyDirectChildRows() { Table table = this.page.getComplexTableWithFoot(); TableBody tbody = table.getBody();/* w ww . j a va 2 s . c o m*/ assertEquals(tbody.getRows().size(), 2); assertEquals(tbody.getRow(0).getCells().size(), 2); assertEquals(tbody.getRow(1).getCells().size(), 2); Table innerTable = tbody.getRow(0).getCell(1).findElement(Table.class, By.tagName("table")); TableBody innerTableBody = innerTable.getBody(); assertEquals(innerTableBody.getRows().size(), 2); assertEquals(innerTableBody.getRow(0).getCells().size(), 2); assertEquals(innerTableBody.getRow(1).getCells().size(), 2); }
From source file:com.gkopevski.fuelcheck.FuelCheckForm.java
/** * This method will crawl data from a web site. When data is fetched will be * compared with the latest get data from the database; If this data is * newer it will be stored in the variable latest data which will be from * type @FuelEntry. If the new data is newer will be passed for printing and * it will be printed;/*from w w w .j a v a2 s .c o m*/ * */ private void crawlData() { try { // Create a new instance of the html unit driver // Notice that the remainder of the code relies on the interface, // not the implementation. driver = new PhantomJSDriver(); System.out.println("URL: " + Constants.BASE_URL + "/" + Constants.LOGIN_HTML); driver.get(Constants.BASE_URL + "/" + Constants.LOGIN_HTML); driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); WebElement element = driver.findElement(By.id("Field__UserLogin")); element.sendKeys(Constants.USERNAME); element = driver.findElement(By.id("Field__UserPassword")); element.sendKeys(Utility.generatePassword()); element = driver.findElement(By.xpath("(//div[@id='DivMenu']/table/tbody/tr/td)[1]")); element.click(); driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); // And now use this to visit Google // driver.get(Constants.BASE_URL + "/" + Constants.OVERVIEW_HTML); // Find the text input element by its name WebElement baseTable = driver.findElement( By.xpath("(//tr[@id='__DashBoardGroup21']/td/table[@id='__DashBoardGroup21']/tbody/tr)[3]")); List<WebElement> rawEntry = baseTable.findElements(By.tagName("td")); FuelEntry tempFuelEntry = new FuelEntry(); String[] quantityUnit = rawEntry.get(8).getText().split(" "); tempFuelEntry.setQuantity(new BigDecimal(quantityUnit[0])); tempFuelEntry.setMeasurementUnit(quantityUnit[1]); SimpleDateFormat dt = new SimpleDateFormat("dd/MM/yyyyy hh:mm:ss"); Date date = dt.parse(rawEntry.get(6).getText()); if (latestFE == null || (date.after(latestFE.getStartDate()) && tempFuelEntry.getQuantity().compareTo(BigDecimal.ZERO) > 0)) { tempFuelEntry.setDriver(rawEntry.get(2).getText()); tempFuelEntry.setVehicle(rawEntry.get(3).getText()); tempFuelEntry.setProduct(rawEntry.get(4).getText()); tempFuelEntry.setDispenzer(Integer.valueOf(rawEntry.get(5).getText())); tempFuelEntry.setStartDate(date); fuelEntryService.saveFuelEntry(tempFuelEntry); latestFE = tempFuelEntry; PrintFuelEntry pfe = new PrintFuelEntry(); pfe.printLatestEntry(); } driver.quit(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.google.testing.web.screenshotter.ScreenshotterTest.java
License:Apache License
@Test public void ofInputElement() throws Exception { Screenshot ss = screenshotter.of(driver.findElement(By.tagName("input"))).takeScreenshot(); Path out = tmpDir.resolve("ofInputElement.png"); Files.write(out, ss.asBytes()); System.out.println("image written to: " + out); Image img = ss.asImage();// w w w . j a va2 s .c o m assertEquals(200, img.getHeight(null)); assertEquals(200, img.getWidth(null)); }
From source file:com.googlecode.fightinglayoutbugs.DetectElementsWithInvisibleFocus.java
License:Apache License
@Nullable private FocusedElement focusFirstElement(WebPage webPage) { WebElement firstFocusedWebElement = getFocusedWebElement(webPage); if (firstFocusedWebElement == null) { // Try to focus first element ... try {//from w w w .j a v a2 s . com WebDriver driver = webPage.getDriver(); WebElement bodyElement = driver.findElement(By.tagName("body")); bodyElement.sendKeys(Keys.TAB); } catch (Exception e) { LOG.warn("Failed to focus first element.", e); } firstFocusedWebElement = getFocusedWebElement(webPage); } else if ("body".equals(firstFocusedWebElement.getTagName().toLowerCase())) { firstFocusedWebElement.sendKeys(Keys.TAB); firstFocusedWebElement = getFocusedWebElement(webPage); } if (firstFocusedWebElement != null && !"body".equals(firstFocusedWebElement.getTagName().toLowerCase())) { return toFocusedElement(firstFocusedWebElement, webPage); } else { return null; } }
From source file:com.googlecode.fightinglayoutbugs.DetectInvalidImageUrls.java
License:Apache License
private void checkVisibleImgElements() { int numImgElementsWithoutSrcAttribute = 0; int numImgElementsWithEmptySrcAttribute = 0; final Set<String> seen = new HashSet<String>(); for (WebElement img : _webPage.findElements(By.tagName("img"))) { if (img.isDisplayed()) { final String src = img.getAttribute("src"); if (src == null) { ++numImgElementsWithoutSrcAttribute; } else if ("".equals(src)) { ++numImgElementsWithEmptySrcAttribute; } else { if (seen.add(src)) { try { checkImageUrl(src, "Detected visible <img> element with invalid src attribute \"" + src + "\""); } catch (MalformedURLException e) { addLayoutBugIfNotPresent("Detected visible <img> element with invalid src attribute \"" + src + "\" -- " + e.getMessage()); }//from w w w.j ava2 s . c om } } } } if (numImgElementsWithEmptySrcAttribute > 0) { if (numImgElementsWithEmptySrcAttribute == 1) { addLayoutBugIfNotPresent("Detected visible <img> element with empty src attribute."); } else { addLayoutBugIfNotPresent("Detected " + numImgElementsWithEmptySrcAttribute + " visible <img> elements with empty src attribute."); } } if (numImgElementsWithoutSrcAttribute > 0) { if (numImgElementsWithEmptySrcAttribute == 1) { addLayoutBugIfNotPresent("Detected visible <img> without src attribute."); } else { addLayoutBugIfNotPresent("Detected " + numImgElementsWithoutSrcAttribute + " visible <img> elements without src attribute."); } } }
From source file:com.googlecode.fightinglayoutbugs.DetectInvalidImageUrls.java
License:Apache License
private void checkStyleElements() { for (WebElement styleElement : _webPage.findElements(By.tagName("style"))) { final String css = (String) _webPage.executeJavaScript("return arguments[0].innerHTML", styleElement); for (String importUrl : getImportUrlsFrom(css)) { checkCssResourceAsync(importUrl + " (imported in <style> element)", importUrl, _baseUrl, _documentCharset);// www.jav a 2s. co m } for (String url : extractUrlsFrom(css)) { try { checkImageUrl(url, "Detected <style> element with invalid image URL \"" + url + "\""); } catch (MalformedURLException e) { addLayoutBugIfNotPresent( "Detected <style> element with invalid image URL \"" + url + "\" -- " + e.getMessage()); } } } }
From source file:com.googlecode.fightinglayoutbugs.DetectInvalidImageUrls.java
License:Apache License
private void checkLinkedCss() { for (WebElement link : _webPage.findElements(By.tagName("link"))) { String rel = link.getAttribute("rel"); if (rel != null) { rel = rel.toLowerCase(Locale.ENGLISH); }/*from ww w. j av a 2s .c o m*/ final String type = link.getAttribute("type"); final String href = link.getAttribute("href"); if ((rel != null && rel.contains("stylesheet")) || (type != null && type.startsWith("text/css"))) { if (href != null) { String charset = link.getAttribute("charset"); if (!isValidCharset(charset)) { charset = _documentCharset; } checkCssResourceAsync(href, href, _baseUrl, charset); } } // prepare checkFavicon ... if (rel != null && ("icon".equals(rel) || "shortcut icon".equals(rel))) { if (href != null) { _faviconUrl = href; } } } }