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

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. 

主站蜘蛛池模板: 吉木乃县| 神木县| 乌兰浩特市| 江永县| 彰化市| 汾阳市| 会理县| 桃园市| 鄂伦春自治旗| 忻城县| 荔波县| 曲阳县| 阜城县| 房产| 油尖旺区| 博罗县| 蒙阴县| 九江县| 泰宁县| 灵丘县| 礼泉县| 平南县| 芦山县| 桂阳县| 东辽县| 天柱县| 遂宁市| 元阳县| 安化县| 和林格尔县| 二连浩特市| 措勤县| 剑阁县| 西城区| 冷水江市| 绥中县| 河南省| 新晃| 双鸭山市| 云林县| 旌德县|