Java Code Examples for org.openqa.selenium.WebElement.findElements()

The following are Jave code examples for showing how to use findElements() of the org.openqa.selenium.WebElement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Java_Good   File: ContactHelper.java   Source Code and License Vote up 8 votes
public Contacts all() {
    if (contactCache != null) {
        return new Contacts(contactCache);
    }
    contactCache = new Contacts();
    List<WebElement> elements = wd.findElements(By.xpath("//tr[@name='entry']"));
    for (WebElement element : elements) {
        List<WebElement> contactEntries = element.findElements(By.cssSelector("td"));
        String firstname = contactEntries.get(2).getText();
        String lastname = contactEntries.get(1).getText();
        String allPhones = contactEntries.get(5).getText();
        String allEmails = contactEntries.get(4).getText();
        String address = contactEntries.get(3).getText();
        int id = Integer.parseInt(element.findElement(By.tagName("input")).getAttribute("value"));
        contactCache.add(new ContactData().withId(id).withFirstname(firstname).withLastname(lastname)
                .withAllPhones(allPhones).withAllEmails(allEmails).withAddress(address));
    }
    return new Contacts(contactCache);
}
 
Example 2
Project: zucchini   File: ClickStep.java   Source Code and License Vote up 6 votes
@When("^I click on element having (id|name|class|xpath|css) \"([^\"]*)\" on row containing \"([^\"]*)\" at table with (id|name|class|xpath|css) \"([^\"]*)\"$")
public void clickElementHavingOnRow(String type, String element, String text, String tableType, String tableElement) {
    WebElement table = getWebElement(tableType, tableElement);
    List<WebElement> rows = table.findElements(By.tagName("tr"));

    WebElement row = findTextAtRow(text, rows);
    if(row != null){
        WebElement el = row.findElement(TypeEnum.getBy(type, element));
        el.click();
    }else {
        Assert.fail(String.format("Didn't found %s on row",text));
    }
}
 
Example 3
Project: mot-automated-testsuite   File: WebDriverWrapper.java   Source Code and License Vote up 6 votes
/**
 * Checks whether a table, identified by having a heading with the specified text, has at least the specified
 * number of rows (ignoring any heading rows).
 * @param headingText       The table heading text to look for
 * @param rows              The minimum number of rows the table must have
 * @return <code>true</code> if the table has at least <code>rows</code> rows.
 */
public boolean checkTableHasRows(String headingText, int rows) {
    try {
        // find the table with a heading containing the specified text...
        WebElement tableElement = webDriver.findElement(
                By.xpath("//th[contains(text(),'" + headingText + "')]//ancestor::table[1]"));

        // then count the number of rows in the table...
        List<WebElement> rowElements = tableElement.findElements(By.tagName("tr"));

        // is the number of rows (minus the heading row) at lest the specified amount?
        return (rowElements.size() - 1) >= rows;

    } catch (NoSuchElementException ex) {
        return false;
    }
}
 
Example 4
Project: Cognizant-Intelligent-Test-Scripter   File: CommonMethods.java   Source Code and License Vote up 6 votes
public HashMap<String, String> getCellValue(WebElement Element, int tr,
        int td) {
    int rowCounter = 0;
    int colCounter = 0;
    String rowKey = null;
    String colKey = null;
    HashMap<String, String> HashTable = new HashMap<>();

    String strObj = Data;
    List<WebElement> tableList = Element.findElements(By
            .cssSelector("div[class='" + strObj + "'] tr td"));
    for (WebElement listIterator : tableList) {
        String TagName = listIterator.getTagName();
        if (TagName.equals("tr")) {
            rowKey = "R" + rowCounter++;
        }
        if (TagName.equals("td")) {
            colKey = "C" + colCounter++;
        }
        HashTable.put(rowKey + colKey, listIterator.getText());
    }
    return HashTable;
}
 
Example 5
Project: ServiceNow_Selenium   File: ServiceNow.java   Source Code and License Vote up 5 votes
/**
 * @category Set Element
 */
private void typeInElement(WebElement element, String content) {
	_action.moveToElement(element);
	switch (this.getFieldType(element)) {
	case "choice":
		List<WebElement> options = element.findElements(By.tagName("option"));
		for (int i = 0; i < options.size(); i++) {
			if (options.get(i).getText().trim().equalsIgnoreCase(content)) {
				element.click();
				options.get(i).click();
				break;
			}
		}
		break;
	default:
		_action.click();
		if(!this.getFieldValueByID(element.getAttribute("id")).equalsIgnoreCase("")) {
			//TODO - Find a faster way to do this.
			_action.sendKeys(Keys.END);
			for (int i = 0; i <this.getFieldValueByID(element.getAttribute("id")).length(); i ++) {
				_action.sendKeys(Keys.BACK_SPACE);
			}

		}
		_action.sendKeys(content);
		_action.sendKeys(Keys.TAB);
	_action.perform();
	}

}
 
Example 6
Project: selenium-testng-template   File: SeleniumUtils.java   Source Code and License Vote up 5 votes
/**
 * Example: getRowTextsByColumnText(table, 2, '22') returns row {11, 22, 33}
    * 1	2	3
    * 11	22	33
    * 111	222	333
 * 
 * @param table
 * @param columnIndex Start from 1
 * @param searchText
 * @return
 */
public static final List<String> getTableRowByColumnText(WebElement table, int columnIndex, String searchText) {
	if (!table.getTagName().equalsIgnoreCase("table")) {
   		throw new IllegalArgumentException("Table web element is required");
   	}

	WebElement rowElement = table.findElement(By.xpath("//tr//td[position()=" + columnIndex + " and contains(*|text(), '" + searchText + "')]/.."));
   	List<WebElement> elements = rowElement.findElements(By.tagName("td"));
   	List<String> result = new ArrayList<String>(elements.size());
   	for (WebElement e : elements) {
   		result.add(StringUtils.trim(e.getText()));
   	}
   	return result;
}
 
Example 7
Project: marathonv5   File: JTreeDynamicTreeTest.java   Source Code and License Vote up 5 votes
public void getAllNodes() throws Throwable {
    expandTree();
    WebElement tree = page.getTree();
    tree.click();
    List<WebElement> nodes = tree.findElements(By.cssSelector(".::all-nodes"));
    AssertJUnit.assertEquals(7, nodes.size());
}
 
Example 8
Project: marathonv5   File: JTreeDynamicTreeTest.java   Source Code and License Vote up 5 votes
public void getNodesByText() throws Throwable {
    WebElement tree = page.getTree();
    tree.click();
    AssertJUnit.assertEquals("[\"Root Node\"]", tree.getText());
    expandTree();
    AssertJUnit.assertEquals("[\"Root Node\",\"Parent 1\",\"Child 1\",\"Child 2\",\"Parent 2\",\"Child 1\",\"Child 2\"]",
            tree.getText());
    List<WebElement> nodes;
    nodes = tree.findElements(By.cssSelector(".::all-nodes[text='Root Node']"));
    AssertJUnit.assertEquals(1, nodes.size());
    nodes = tree.findElements(By.cssSelector(".::all-nodes[text='Child 2']"));
    AssertJUnit.assertEquals(2, nodes.size());
}
 
Example 9
Project: marathonv5   File: JListXTest.java   Source Code and License Vote up 5 votes
public void listGetAnItemFromListWebelement() throws Throwable {
    driver = new JavaDriver();
    WebElement list = driver.findElement(By.name("list-1"));
    WebElement item21 = list.findElement(By.cssSelector(".::nth-item(21)"));
    assertEquals("List Item - 21", item21.getText());
    List<WebElement> allItems = list.findElements(By.cssSelector(".::all-items"));
    assertEquals(30, allItems.size());
}
 
Example 10
Project: marathonv5   File: JavaDriverTest.java   Source Code and License Vote up 5 votes
public void findElementsOfElement() throws Throwable {
    driver = new JavaDriver();
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override public void run() {
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
    WebElement element = driver.findElement(By.name("box-panel"));
    AssertJUnit.assertNotNull(element);
    List<WebElement> clickMe = element.findElements(By.name("click-me"));
    AssertJUnit.assertNotNull(clickMe);
}
 
Example 11
Project: AlipayAuto   File: AlipayAuto.java   Source Code and License Vote up 5 votes
private static String getOppositeUser(String transactionNo) {
	// ��ȡ�ؼ��ֶ�Ӧ��������
	WebElement keywordInput = driver.findElement(By.id("J-keyword"));
	keywordInput.clear();
	keywordInput.sendKeys(transactionNo);
	WebElement keywordSelect = driver.findElement(By.id("keyword"));
	List<WebElement> options = keywordSelect.findElements(By.tagName("option"));
	// until������ʾֱ���ɵ��ٵ�
	// WebElement selectElement = wait.until(ExpectedConditions
	// .visibilityOfElementLocated(By.id("keyword")));
	// ��Ҫִ��JavaScript��䣬����ǿתdriver
	JavascriptExecutor js = (JavascriptExecutor) driver;
	// Ҳ������ô��setAttribute("style","");
	js.executeScript("document.getElementById('keyword').style.display='list-item';");
	js.executeScript("document.getElementById('keyword').removeAttribute('smartracker');");
	js.executeScript("document.getElementById('keyword').options[1].selected = true;");
	js.executeScript("document.getElementById('J-select-range').style.display='list-item';");
	// ���ý���ʱ��ѡ��
	Select selectTime = new Select(driver.findElement(By.id("J-select-range")));
	selectTime.selectByIndex(3);// ѡ�е������������
	System.out.println("selectTime.isMultiple() : " + selectTime.isMultiple());
	// ���ùؼ���ѡ��
	Select selectKeyword = new Select(driver.findElement(By.id("keyword")));
	// selectKeyword.selectByValue("bizInNo");//�˴���value��д<option>��ǩ�е�valueֵ
	selectKeyword.selectByIndex(1);// ѡ�е��ǽ��׺�
	System.out.println("selectKeyword.isMultiple() : " + selectKeyword.isMultiple());
	WebElement queryButton = driver.findElement(By.id("J-set-query-form"));// �õ�������ť
	// ���������ť
	queryButton.submit();
	WebElement tr = driver.findElement(By.id("J-item-1"));// �Ȼ�ȡtr
	WebElement td = tr.findElement(By.xpath("//*[@id=\"J-item-1\"]/td[5]/p[1]"));
	return td.getText();
}
 
Example 12
Project: selenium-testng-template   File: SeleniumUtils.java   Source Code and License Vote up 5 votes
/**
    * Example: getTableTextsByColumnIndex(table, 2) returns {2, 22, 22}
    * 1	2	3
    * 11	22	33
    * 111	222	333
    * 
    * @param tablert5
    * @return
    */
public static final List<String> getTableTextsByColumnIndex(WebElement table, int columnIndex) {
   	if (!table.getTagName().equalsIgnoreCase("table")) {
   		throw new IllegalArgumentException("Table web element is required");
   	}
   	
   	List<WebElement> elements = table.findElements(By.cssSelector("tr td:nth-child(" + columnIndex + ")"));
   	List<String> result = new ArrayList<String>(elements.size());
   	for (WebElement e : elements) {
   		result.add(StringUtils.trim(e.getText()));
   	}
   	return result;
   }
 
Example 13
Project: selenium-testng-template   File: SeleniumUtils.java   Source Code and License Vote up 5 votes
public static final void unselectCheckbox(WebElement parent, int index){
	List<WebElement> checkBox= parent.findElements(By.cssSelector("input[type='checkbox']"));
	if(checkBox.get(index).isSelected()){
		checkBox.get(index).click();
		SeleniumUtils.sleepInSeconds(2);
	}
}
 
Example 14
Project: selenium-testng-template   File: SeleniumUtils.java   Source Code and License Vote up 5 votes
/**
    * Example: getTableTextsByRowIndex(table, 2) returns {11, 22, 33}
    * 1	2	3
    * 11	22	33
    * 111	222	333
    * 
    * @param table
    * @param rowIndex Start from 1.
    * @return
    */
public static final List<String> getTableTextsByRowIndex(WebElement table, int rowIndex) {
   	if (!table.getTagName().equalsIgnoreCase("table")) {
   		throw new IllegalArgumentException("Table web element is required");
   	}
   	
   	List<WebElement> elements = table.findElements(By.cssSelector("tr:nth-child(" + rowIndex + ") td"));
   	log.info(elements.size()+":");
   	List<String> result = new ArrayList<String>(elements.size());
   	for (WebElement e : elements) {
   		result.add(StringUtils.trim(e.getText()));
   	}
   	System.out.println(Arrays.asList(result));
   	return result;
   }
 
Example 15
Project: vu-timetable   File: Navigator.java   Source Code and License Vote up 5 votes
private void selectSemester() {
    final WebElement selectElement = driver.findElement(By.id("lbWeeks"));
    for (WebElement option : selectElement.findElements(By.tagName("option"))) {
        if (option.getText().toLowerCase().contains("semester")) {
            option.click();
            return;
        }
    }
}
 
Example 16
Project: selenium-testng-template   File: SeleniumUtils.java   Source Code and License Vote up 5 votes
public static final void selectRadioButton(WebElement parent,int index){
   	SeleniumWaitUtils.waitForElement(By.cssSelector("input[type='radio']"));
   	List<WebElement> radioButton= parent.findElements(By.cssSelector("input[type='radio']"));
   	if(!radioButton.get(index).isSelected()){
   		radioButton.get(index).click();
   	}
}
 
Example 17
Project: saladium   File: SaladiumDriver.java   Source Code and License Vote up 5 votes
@Override
public void selectionner(String type, String selector, String valeur) {
    this.logger.info("selectionner(type" + type + ", selector" + selector + ", valeur" + valeur + ")");
    try {
        By locator = BySelec.get(type, selector);
        WebElement elem = wait.until(ExpectedConditions.presenceOfElementLocated(locator));
        String tagName = elem.getTagName();
        if (tagName.equals("select")) {
            Select select = new Select(elem);
            // List<WebElement> options = select.getAllSelectedOptions();
            // Iterator<WebElement> it = options.iterator();
            // while (it.hasNext()) {
            // WebElement webElement = it.next();
            // if ("auto".equals(webElement.getCssValue("z-index"))) {
            // if (!webElement.isDisplayed()) {
            // System.out.println("################### !isDisplayed()");
            // }
            // System.out.println("################### cssValue " + webElement.getCssValue("z-index"));
            // }
            // }
            select.selectByVisibleText(valeur);
            // Thread.sleep(1000);
        } else {
            elem.click();
            List<WebElement> children = elem.findElements(
                By.xpath("//div[@class='listComboBoxElement']/li"));
            Iterator<WebElement> it = children.iterator();
            boolean flag = false;
            do {
                WebElement webElement = it.next();
                if (webElement.getText().startsWith(valeur)) {
                    if (webElement.isDisplayed()) {
                        webElement.click();
                        flag = true;
                    }
                }
            } while (!flag && it.hasNext());
            if (!flag) {
                Assert.fail("Impossible de trouver la valeur :" + valeur
                    + " pour le champs :" + type + ":" + selector);
            }
        }
    } catch (NoSuchElementException | TimeoutException e) {
        String pathScreenShot = takeScreenShot();
        Assert.fail("Sélection impossible ! (type" + type + ", selector" + selector + ", valeur" + valeur + ") pathScreenShot=" + pathScreenShot);
    }
}
 
Example 18
Project: SilkAppDriver   File: WpfSampleAppTests.java   Source Code and License Vote up 5 votes
@Test
public void testFindElements() {
	driver.findElement(By.xpath("//WPFMenuItem[@caption='Controls']")).click();
	driver.findElement(By.xpath("//WPFMenuItem[@caption='Basic Controls']")).click();

	WebElement basicControlsWindow = driver.findElement(By.xpath("/WPFWindow[@caption='Basic Controls']"));
	basicControlsWindow
			.findElement(By.xpath("//WPFTabControl[@automationId='tabControl']/WPFTabItem[@caption='Button']"))
			.click();

	// find elements
	List<WebElement> buttons = driver.findElements(By.xpath("//WPFButton"));

	assertEquals(6, buttons.size());

	WebElement buttonA = buttons.get(0);

	assertEquals("Button _A", buttonA.getText());
	assertEquals("com.microfocus.silktest.jtf.wpf.WPFButton", buttonA.getTagName());

	WebElement textField = driver.findElement(By.xpath("//WPFTextBox"));
	assertEquals("", textField.getText());
	buttonA.click();
	assertEquals("Button A pressed", textField.getText());

	// find elements from element
	List<WebElement> textBoxes = basicControlsWindow.findElements(By.xpath("//WPFTextBox"));
	assertEquals(2, textBoxes.size());

	// find non existent elements
	List<WebElement> shouldBeEmpty = driver.findElements(By.xpath("//SAPButton"));

	assertEquals(0, shouldBeEmpty.size());
}
 
Example 19
Project: satisfy   File: BaseTableSteps.java   Source Code and License Vote up 4 votes
private List<WebElement> getCellsFor(WebElement row) {
    return row.findElements(By.xpath("./td"));
}
 
Example 20
Project: selenium-testng-template   File: SeleniumUtils.java   Source Code and License Vote up 4 votes
public static final void selectCheckbox(WebElement parent, int index){
   	List<WebElement> checkBox= parent.findElements(By.cssSelector("input[type='checkbox']"));
 	if(!checkBox.get(index).isSelected()){
  	checkBox.get(index).click();
 	}
}