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

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.
主站蜘蛛池模板: 皮山县| 高台县| 鄂托克前旗| 怀来县| 桑日县| 晋城| 花莲市| 绿春县| 丰县| 萝北县| 格尔木市| 资阳市| 射阳县| 和田县| 大安市| 华容县| 靖宇县| 岳普湖县| 丰都县| 体育| 万盛区| 黄平县| 怀远县| 怀集县| 宜良县| 新营市| 开化县| 宁化县| 新化县| 藁城市| 镇坪县| 英超| 荔浦县| 怀集县| 岳西县| 永修县| 通许县| 兴文县| 汉中市| 砚山县| 贵南县|