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

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.

主站蜘蛛池模板: 皋兰县| 广南县| 中西区| 咸阳市| 东海县| 肇庆市| 太保市| 霍林郭勒市| 开平市| 敦煌市| 武安市| 广宗县| 葵青区| 宁强县| 唐海县| 隆林| 锦州市| 繁昌县| 海城市| 威宁| 贺州市| 麻城市| 吉林省| 夹江县| 万年县| 永济市| 卓尼县| 敦煌市| 长武县| 南平市| 手游| 富川| 新干县| 札达县| 西平县| 乌鲁木齐市| 理塘县| 阿拉善右旗| 潞西市| 报价| 兰溪市|