- Selenium WebDriver 3 Practical Guide
- Unmesh Gundecha Satya Avasarala
- 195字
- 2021-08-13 16:08:08
The By.cssSelector() method
The By.cssSelector() method is similar to the By.xpath() method in its usage, but the difference is that it is slightly faster than the By.xpath locating mechanism. The following are the commonly used syntaxes to identify elements:
- To identify an element using the div element with the #flrs ID, we use the #flrs syntax
- To identify the child anchor element, we use the #flrs > a syntax, which will return the link element
- To identify the anchor element with its attribute, we use the #flrs > a[a[href="/intl/en/about.html"]] syntax
Let's try to modify the previous code, which uses the XPath locating mechanism to use the cssSelector mechanism:
@Test
public void byCssSelectorLocatorExample() {
WebElement searchBox =
driver.findElement(By.cssSelector("#search"));
searchBox.sendKeys("Bags");
searchBox.submit();
assertThat(driver.getTitle())
.isEqualTo("Search results for: 'Bags'");
}
The preceding code uses the By.cssSelector locating mechanism, which uses the css selector ID of the Search box.
Let's look at a slightly complex example. We will try to identify the About Us on the Homepage:
@Test
public void byCssSelectorLocatorComplexExample() {
WebElement aboutUs =
driver.findElement(By
.cssSelector("a[href*='/about-magento-demo-store/']"));
aboutUs.click();
assertThat(driver.getTitle())
.isEqualTo("About Us");
}
The preceding code uses the cssSelector() method to find the anchor element identified by its href attribute.
推薦閱讀
- CorelDRAW X6 中文版圖形設(shè)計(jì)實(shí)戰(zhàn)從入門到精通
- 6G潛在關(guān)鍵技術(shù)(下冊)
- 網(wǎng)管員典藏書架:網(wǎng)絡(luò)管理與運(yùn)維實(shí)戰(zhàn)寶典
- 物聯(lián)網(wǎng)時(shí)代
- 物聯(lián)網(wǎng)之霧:基于霧計(jì)算的智能硬件快速反應(yīng)與安全控制
- Mastering Dart
- SAE原理與網(wǎng)絡(luò)規(guī)劃
- 紅藍(lán)攻防:構(gòu)建實(shí)戰(zhàn)化網(wǎng)絡(luò)安全防御體系
- 組網(wǎng)技術(shù)與網(wǎng)絡(luò)管理
- 云工廠:開啟中國制造云時(shí)代
- 商業(yè)的本質(zhì)和互聯(lián)網(wǎng)
- Enterprise ApplicationDevelopment with Ext JSand Spring
- 趣話通信:6G的前世、今生和未來
- 物聯(lián)網(wǎng)傳感器技術(shù)與應(yīng)用
- Learning IoT with Particle Photon and Electron