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

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.

主站蜘蛛池模板: 夏津县| 新兴县| 浦城县| 乌拉特前旗| 汾西县| 长宁县| 青田县| 蒲城县| 平邑县| 资源县| 虎林市| 周口市| 菏泽市| 正阳县| 平舆县| 金溪县| 孟州市| 南充市| 甘肃省| 临西县| 南安市| 乌拉特后旗| 舞钢市| 紫阳县| 科技| 临江市| 汤原县| 陆良县| 勐海县| 昌宁县| 富阳市| 宝坻区| 望都县| 漯河市| 隆子县| 通江县| 准格尔旗| 安泽县| 奉新县| 文山县| 海城市|