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

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. 

主站蜘蛛池模板: 土默特左旗| 秀山| 云林县| 通山县| 苏尼特右旗| 体育| 新丰县| 临城县| 威远县| 绥滨县| 开江县| 民乐县| 昭通市| 奈曼旗| 贵港市| 安塞县| 河西区| 平度市| 天峨县| 喀什市| 陵川县| 句容市| 抚宁县| 策勒县| 阳原县| 五台县| 兴城市| 齐齐哈尔市| 富阳市| 肃宁县| 宽城| 渭源县| 龙川县| 望江县| 曲阳县| 锡林郭勒盟| 宁武县| 晋州市| 新干县| 台湾省| 广安市|