链接文本和部分链接文本 Selenium
链接文本 Selenium?
A 链接文本 Selenium 用于识别网页上的超链接。它通过锚标记来确定。为了在网页上创建超链接,我们可以使用锚标记,后跟链接文本。
符合条件的链接
可以使用链接文本的完全匹配或部分匹配来访问链接。下面的示例提供了存在多个匹配的场景,并解释了 WebDriver 如何处理它们。
在本教程中,我们将学习使用 Webdriver 查找和访问链接的可用方法。此外,我们将讨论访问链接时遇到的一些常见问题,并进一步讨论如何解决这些问题。
完整链接文本 Selenium – 通过.linkText()
通过 By.linkText() 方法使用其精确的链接文本访问链接。但是,如果有两个链接具有完全相同的链接文本,则此方法将仅访问第一个链接。请考虑以下 HTML 代码
当你尝试运行下面的 WebDriver 代码时,你将访问第一个“单击此处”链接
代码:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class MyClass { public static void main(String[] args) { String baseUrl = "https://demo.guru99.com/test/link.html"; System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get(baseUrl); driver.findElement(By.linkText("click here")).click(); System.out.println("title of page is: " + driver.getTitle()); driver.quit(); } }
下面是它的工作原理-
结果,您将自动进入 Google。
完成部分链接文本 Selenium – By.partialLinkText()
使用链接文本的一部分来访问链接是使用 通过.partialLinkText() 方法。如果您指定具有多个匹配项的部分链接文本,则只会访问第一个匹配项。请考虑下面的 HTML 代码。
当你执行下面的 WebDriver 代码时,你仍会被带到 Google。
代码:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class P1 { public static void main(String[] args) { String baseUrl = "https://demo.guru99.com/test/accessing-link.html"; System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get(baseUrl); driver.findElement(By.partialLinkText("here")).click(); System.out.println("Title of page is: " + driver.getTitle()); driver.quit(); } }
如何获取具有相同链接文本的多个链接
那么,如何解决上述问题呢?如果有多个链接具有相同的链接文本,并且我们想访问除第一个链接之外的其他链接,我们该怎么做呢?
在这种情况下,通常使用不同的定位器,即...By.xpath()、By.cssSelector() 或 By.tagName()。
最常用的是 By.xpath()。它是最可靠的,但它看起来很复杂且不可读。
链接文本区分大小写
参数为 通过.linkText() 和 通过.partialLinkText() 都是区分大小写的,也就是说,大小写很重要。例如, Mercury Tours 的主页上,有两个链接包含文本“egis” - 一个是位于顶部菜单的“注册”链接,另一个是位于页面右下角的“在此注册”链接。
虽然两个链接都包含字符序列“egis”,但其中一个是“By.partialLinkText()”方法将根据字符的大小写分别访问这两个链接。请参阅下面的示例代码。
代码
public static void main(String[] args) { String baseUrl = "https://demo.guru99.com/test/newtours/"; System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get(baseUrl); String theLinkText = driver.findElement(By .partialLinkText("egis")) .getText(); System.out.println(theLinkText); theLinkText = driver.findElement(By .partialLinkText("EGIS")) .getText(); System.out.println(theLinkText); driver.quit(); }
区块内外的链接
最新的 HTML5 标准允许将标签放置在块级标签(如、 或 )的内部和外部。“By.linkText()”和“By.partialLinkText()”方法可以访问位于这些块级元素外部和内部的链接。请考虑下面的 HTML 代码。
下面的 WebDriver 代码使用 By.partialLinkText() 方法访问这两个链接。
代码:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class MyClass { public static void main(String[] args) { String baseUrl = "https://demo.guru99.com/test/block.html"; System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get(baseUrl); driver.findElement(By.partialLinkText("Inside")).click(); System.out.println(driver.getTitle()); driver.navigate().back(); driver.findElement(By.partialLinkText("Outside")).click(); System.out.println(driver.getTitle()); driver.quit(); } }
上面的输出确认两个链接都已成功访问,因为它们各自的页面标题都已正确检索。
结语
- 使用 click() 方法访问链接。
- 除了适用于任何 WebElement 的定位器之外,链接还具有基于链接文本的定位器:
- 通过.linkText() – 根据作为参数提供的链接文本的精确匹配来定位链接。
- 通过.partialLinkText() – 根据链接文本的部分文本匹配来定位链接。
- 以上两个定位器均区分大小写。
- 如果有多个匹配项,By.linkText() 和 By.partialLinkText() 将仅选择第一个匹配项。在存在多个具有相同链接文本的链接的情况下,将使用基于 xpath、CSS 的其他定位器。
- findElements() & By.tagName(“a”) 方法查找页面中符合定位器条件的所有元素
- 无论链接是在块级元素内部还是外部,都可以通过 By.linkText() 和 By.partialLinkText() 访问。