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

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. 

主站蜘蛛池模板: 巴东县| 明水县| 盖州市| 绿春县| 那坡县| 贺兰县| 巴南区| 石渠县| 霍林郭勒市| 泾源县| 惠水县| 五峰| 全椒县| 黄冈市| 蚌埠市| 隆子县| 永寿县| 平昌县| 射洪县| 孟村| 潞城市| 揭东县| 隆林| 宁远县| 朔州市| 图们市| 津南区| 射洪县| 漠河县| 杭锦旗| 乌兰察布市| 嵩明县| 噶尔县| 瑞金市| 仪征市| 海口市| 正安县| 兴山县| 通州市| 泌阳县| 西安市|