Key points to Accessing Links & Tables using Selenium WebDriver
- Accessing links using their exact match is done using By.linkText() method.
- Accessing links using their partial match is done using By.partialLinkText() method.
- If there are multiple matches, By.linkText() and By.partialLinkText() will only select the first match.
- Pattern matching using By.linkText() and By.partialLinkText() is case-sensitive.
- The By.tagName("a") method is used to fetch all links within a page.
- Links can be accessed by the By.linkText() and By.partialLinkText() whether they are inside or outside block-level elements.
- Accessing image links are done using By.cssSelector() and By.xpath() methods.
- By.xpath() is commonly used to access table elements.
Links Matching a Criterion
Links can be accessed using an exact or partial match of their link text. The examples below provide scenarios where multiple matches would exist, and would explain how WebDriver would deal with them.
Accessing links using their exact link text is done through the By.linkText() method. However, if there are two links that have the very same link text, this method will only access the first one. Consider the HTML code below
When you try to run the WebDriver code below, you will be accessing the first "click here" link
As a result, you will automatically be taken to Google.
Accessing links using a portion of their link text is done using the By.partialLinkText() method. If you specify a partial link text that has multiple matches, only the first match will be accessed. Consider the HTML code below.
When you execute the WebDriver code below, you will still be taken to Google.