官术网_书友最值得收藏!

Finding elements within the container element

On the http://www.freecrm.com login page, the structure is such that the username, password, and the login button are contained inside the form with id=xyz. In such a situation, the child elements can be accessed using findElements on the container or parent element.

The following code displays the number of input elements in the form:

public class DynamicText1 {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\\SeleniumWD\\src\\main\\resources\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.freecrm.com");
String dynamicXpath = "http://*[@id='loginForm']";
List<WebElement> elem =
driver.findElements(By.xpath(dynamicXpath));
List<WebElement> elem1 =
elem.get(0).findElements(By.tagName("input"));
System.out.println("no of elements: " + elem1.size());
}
}

The output displayed in console is shown below
no of elements: 3

This is a very simple program which has hardcoded values. To remove hardcoding from a program, we require a framework, which we will discuss in forthcoming chapters. 

主站蜘蛛池模板: 内丘县| 三台县| 吉林省| 山阴县| 凌云县| 藁城市| 友谊县| 泰宁县| 彭州市| 康定县| 渝中区| 雷山县| 洛隆县| 林周县| 高安市| 巴林右旗| 延寿县| 两当县| 丰顺县| 长岭县| 大厂| 合阳县| 栾川县| 兰溪市| 平泉县| 调兵山市| 保山市| 敖汉旗| 郎溪县| 临夏市| 禄劝| 阳西县| 晋城| 鹤山市| 桃园市| 成武县| 广元市| 盈江县| 庆元县| 西昌市| 南华县|