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

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. 

主站蜘蛛池模板: 洛扎县| 汨罗市| 香河县| 加查县| 长岛县| 应城市| 广丰县| 洪泽县| 教育| 蓝山县| 云安县| 盘山县| 旬阳县| 三门峡市| 鹰潭市| 红桥区| 织金县| 三明市| 龙里县| 锦州市| 梁河县| 信丰县| 新乐市| 新民市| 陇川县| 屏东市| 淮滨县| 长阳| 原平市| 庆元县| 临高县| 新竹市| 伊春市| 颍上县| 龙岩市| 洛宁县| 县级市| 九江市| 汉沽区| 石泉县| 泗水县|