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

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.

主站蜘蛛池模板: 井研县| 广宗县| 涡阳县| 井冈山市| 鄂州市| 罗江县| 都江堰市| 卫辉市| 太和县| 股票| 德江县| 广东省| 光泽县| 长武县| 三明市| 拉萨市| 太原市| 江城| 洪湖市| 囊谦县| 濮阳县| 成都市| 任丘市| 凤城市| 峨边| 静安区| 大悟县| 永州市| 噶尔县| 汉阴县| 毕节市| 舞钢市| 嫩江县| 仙居县| 奉贤区| 基隆市| 彭阳县| 宜兰县| 永善县| 武强县| 拉萨市|