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

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.

主站蜘蛛池模板: 凤山县| 获嘉县| 石首市| 类乌齐县| 梅州市| 隆德县| 五台县| 通河县| 波密县| 大埔县| 朔州市| 汉寿县| 新化县| 昆山市| 恭城| 安多县| 汤阴县| 图们市| 华池县| 乌兰浩特市| 韩城市| 青海省| 平和县| 班戈县| 黎川县| 茶陵县| 黄冈市| 汾阳市| 沭阳县| 桐城市| 通河县| 乌海市| 乐清市| 防城港市| 望都县| 博白县| 罗江县| 商丘市| 启东市| 穆棱市| 固安县|