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

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.
主站蜘蛛池模板: 天气| 贵南县| 新兴县| 宁乡县| 安新县| 娄底市| 玉田县| 清流县| 饶平县| 黄龙县| 齐齐哈尔市| 自贡市| 房山区| 盱眙县| 双峰县| 平江县| 雅江县| 满城县| 东至县| 南乐县| 神农架林区| 固原市| 普定县| 宝丰县| 黑山县| 孟村| 南昌市| 绥棱县| 宜良县| 鄢陵县| 石景山区| 龙州县| 定兴县| 娄底市| 大化| 新野县| 沙雅县| 湖北省| 麻阳| 吉安县| 怀来县|