- Selenium WebDriver 3 Practical Guide
- Unmesh Gundecha Satya Avasarala
- 159字
- 2021-08-13 16:08:07
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.
推薦閱讀
- 黑客攻防實(shí)戰(zhàn)技術(shù)完全手冊(cè):掃描、嗅探、入侵與防御
- 物聯(lián)網(wǎng)工程規(guī)劃技術(shù)
- 物聯(lián)網(wǎng)短距離無(wú)線通信技術(shù)應(yīng)用與開(kāi)發(fā)
- Twilio Cookbook
- 異構(gòu)基因共表達(dá)網(wǎng)絡(luò)的分析方法
- Hands-On Chatbot Development with Alexa Skills and Amazon Lex
- OpenLayers Cookbook
- Proxmox High Availability
- HTML5 Game development with ImpactJS
- PLC、現(xiàn)場(chǎng)總線及工業(yè)網(wǎng)絡(luò)實(shí)用技術(shù)速成
- 中小型局域網(wǎng)組建、管理與維護(hù)實(shí)戰(zhàn)
- Wireshark網(wǎng)絡(luò)分析就這么簡(jiǎn)單
- 世界互聯(lián)網(wǎng)發(fā)展報(bào)告·2019
- 計(jì)算機(jī)網(wǎng)絡(luò)技術(shù)及應(yīng)用
- 夢(mèng)工廠之材質(zhì)N次方:Maya材質(zhì)手冊(cè)