How to click the hyperlink using Selenium Java

asked 3 months ago

Selenium WebDriver provides locators called link text and partial link text specifically for hyperlinks.

> If the anchor tag (a) has a fixed visible text, you can use the link text locator to click on it.
> If the text is only partially static or changes dynamically, the partial link text locator helps by matching a portion of the text.

Besides these, you can also perform clicks using other locators like CSS selector, XPath, ID, Name, or ClassName.

Navigate to https://www.qafeast.com/demo,click on the Hyperlink tab.


Using Link Text

driver.findElement(By.linkText("Web browser automation")).click();


Using Partial Link Text

driver.findElement(By.partialLinkText("Web browser au")).click();


When the hyperlink opens a new tab, Selenium doesn’t automatically switch to it. You need to handle it manually: follow this link to know how to switch to a new tab: https://www.qafeast.com/selenium-webdriver-questions/how-to-switch-over-window-in-selenium-java