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

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.

主站蜘蛛池模板: 常宁市| 迁西县| 镇雄县| 格尔木市| 天津市| 科技| 紫云| 大理市| 天峻县| 中牟县| 繁昌县| 新竹县| 泸定县| 抚松县| 措勤县| 新宁县| SHOW| 阳曲县| 长沙县| 含山县| 云林县| 伊吾县| 济宁市| 勃利县| 日喀则市| 承德市| 吉水县| 富蕴县| 射阳县| 张家界市| 马山县| 洛扎县| 临高县| 资阳市| 西畴县| 于田县| 博乐市| 囊谦县| 眉山市| 常德市| 湖南省|