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

The By.name() method

As seen earlier, every element on a web page has many attributes. Name is one of them. For instance, the HTML code for the Search box is:

<input id="search" type="search" name="q" value="" class="input-text required-entry" maxlength="128" placeholder="Search entire store here..." autocomplete="off">

Here, name is one of the many attributes of the search box, and its value is q. If we want to identify this search box and set a value in it in your test script, the code will look as follows:

@Test
public void searchProduct() {
// find search box and enter search string
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("Phones");
searchBox.submit();
assertThat(driver.getTitle())
.isEqualTo("Search results for: 'Phones'");
}

If you observe line four, the locating mechanism used here is By.name and the name is q. So, where did we get this name from? As discussed in the previous section, it is the browser developer tools that helped us get the name of the button. Launch Developer tools and use the inspect elements widget to get the attributes of an element.

主站蜘蛛池模板: 张家口市| 武功县| 孟州市| 土默特右旗| 江孜县| 定州市| 镇平县| 蒲江县| 元谋县| 永靖县| 株洲市| 文登市| 岳普湖县| 织金县| 姚安县| 汉寿县| 内丘县| 云浮市| 韶关市| 弥勒县| 区。| 佛教| 河北省| 信宜市| 德惠市| 大同市| 铁岭市| 新宁县| 津南区| 镇雄县| 化德县| 神池县| 泽州县| 巴林左旗| 文山县| 沈丘县| 海门市| 临泉县| 连江县| 苏州市| 六枝特区|