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

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.
主站蜘蛛池模板: 靖江市| 华池县| 新乐市| 平顺县| 通城县| 易门县| 平和县| 化德县| 柘荣县| 新兴县| 垦利县| 凌海市| 沿河| 谢通门县| 乌鲁木齐市| 银川市| 淅川县| 富裕县| 佛学| 饶平县| 衡山县| 简阳市| 乾安县| 沾益县| 任丘市| 开化县| 绥中县| 当雄县| 高雄县| 蒙自县| 敦煌市| 博湖县| 德保县| 米易县| 兴安县| 邵武市| 得荣县| 泸西县| 岳西县| 鹿泉市| 盐亭县|