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

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. 

主站蜘蛛池模板: 金堂县| 安阳市| 新宁县| 昭苏县| 皮山县| 新龙县| 郑州市| 木兰县| 改则县| 福州市| 晋中市| 尖扎县| 津市市| 三都| 扬中市| 若尔盖县| 扬中市| 新津县| 保德县| 峡江县| 广汉市| 任丘市| 宁国市| 南澳县| 高邑县| 南通市| 醴陵市| 江达县| 宿迁市| 罗江县| 隆安县| 龙川县| 通许县| 化德县| 赞皇县| 旅游| 宜兰县| 襄汾县| 合水县| 亚东县| 驻马店市|