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

The By.partialLinkText() method

The By.partialLinkText locating mechanism is an extension of the By.linkText locator. If you are not sure of the entire link text or want to use only part of the link text, you can use this locator to identify the link element. So, let's modify the previous example to use only partial text on the link; in this case, we will use Privacy from the Privacy Policy link in the site footer:

The code would look like this:

@Test
public void byPartialLinkTextLocatorExample() {
WebElement orderAndReturns =
driver.findElement(By.partialLinkText("PRIVACY"));
orderAndReturns.click();
assertThat(driver.getTitle())
.isEqualTo("Privacy Policy");
}

What happens if there are multiple links whose text has Privacy in it? That is a question for the findElement() method rather than the locator. Remember when we discussed the findElement() method earlier, it will return only the first WebElement that it comes across. If you want all the WebElements that contain Privacy in its link text, use the findElements() method, which will return a list of all those elements.

Use WebDriver's findElements() method if you think you need all the WebElements that satisfy a locating-mechanism condition.
主站蜘蛛池模板: 新余市| 泉州市| 琼结县| 陆河县| 白朗县| 石渠县| 缙云县| 静海县| 泾川县| 仁化县| 铜陵市| 壶关县| 白银市| 乐业县| 枣强县| 吉首市| 武安市| 墨脱县| 钟山县| 普格县| 兰州市| 沙田区| 肥西县| 高密市| 陆河县| 菏泽市| 仁怀市| 石屏县| 南溪县| 宝兴县| 淅川县| 普陀区| 潼关县| 萝北县| 额济纳旗| 宝山区| 房产| 雅江县| 黄骅市| 彰化县| 重庆市|