- 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.
- 數據通信網絡實踐:基礎知識與交換機技術
- 連接未來:從古登堡到谷歌的網絡革命
- 網絡故障現場處理實踐(第4版)
- Web Application Development with R Using Shiny
- 物聯網+BIM:構建數字孿生的未來
- 網絡互聯技術(實踐篇)
- SD-WAN架構與技術(第2版)
- 物聯網長距離無線通信技術應用與開發
- 通信十年:擁抱互聯網
- 4G小基站系統原理、組網及應用
- Master Apache JMeter:From Load Testing to DevOps
- 網絡利他行為研究:積極心理學的視角
- 轉化:提升網站流量和轉化率的技巧
- 現場綜合化網絡運營與維護:運營商數字化轉型技術與實踐
- 黑客與反黑工具使用詳解