- Selenium WebDriver 3 Practical Guide
- Unmesh Gundecha Satya Avasarala
- 222字
- 2021-08-13 16:08:07
The By.id() method
On a web page, each element is uniquely identified by an ID attribute, which is optionally provided. An ID can be assigned manually by the developer of the web application or left to be dynamically generated by the application. Dynamically-generated IDs can be changed on every page refresh or over a period of time. Now, consider the HTML code of the Search box:
<input id="search" type="search" name="q" value="" class="input-text required-entry" maxlength="128" placeholder="Search entire store here..." autocomplete="off">
In the preceding code, the id attribute value of the search box is search.
Let's see how to use the ID attribute as a locating mechanism to find the Search box:
@Test
public void byIdLocatorExample() {
WebElement searchBox = driver.findElement(By.id("search"));
searchBox.sendKeys("Bags");
searchBox.submit();
assertThat(driver.getTitle())
.isEqualTo("Search results for: 'Bags'");
}
In preceding code, we used the By.id() method and the search box's id attribute value to find the element.
Here, try to use the By.id identifier, and use the name value (that is, q) instead of the id value (that is, search). Modify line three as follows:
WebElement searchBox = driver.findElement(By.id("q"));
The test script will fail to throw an exception, as follows:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"q"}
WebDriver couldn't find an element by id whose value is q. Thus, it throws an exception saying NoSuchElementException.
- 廣電5G從入門到精通
- Cisco OSPF命令與配置手冊(cè)
- EDA技術(shù)與VHDL編程
- 面向物聯(lián)網(wǎng)的嵌入式系統(tǒng)開(kāi)發(fā):基于CC2530和STM32微處理器
- SAE原理與網(wǎng)絡(luò)規(guī)劃
- 6G無(wú)線網(wǎng)絡(luò)空口關(guān)鍵技術(shù)
- 人人都該都懂的互聯(lián)網(wǎng)思維
- Learning Node.js Development
- 轉(zhuǎn)化:提升網(wǎng)站流量和轉(zhuǎn)化率的技巧
- 新媒體交互藝術(shù)
- NB-IoT原理和優(yōu)化
- 工業(yè)以太網(wǎng)技術(shù):AFDX/TTE網(wǎng)絡(luò)原理、接口、互連與安全
- 想象的互動(dòng):網(wǎng)絡(luò)人際傳播中的印象形成
- React Design Patterns and Best Practices(Second Edition)
- LiveCode Mobile Development Beginner's Guide