Handling hyperlink (<a>) is a basic requirement while navigating with automation in Selenium. This is a complete article on how to click on a hyperlink with Selenium in different manners.
Table of Content
What is Clicking Links with Selenium
Selenium allows you to automate browser actions like clicking, typing, and scraping data. Hyperlinks (<a> tags) are very commonly used for navigation, and it is essential to know how to interact with them.
Common Usage of Clicking Links
- Navigation to different pages of a website.
- Verifying whether clicking on a link will navigate you to the correct URL.
- Automating login operations or pagination.
How to Click an <a> Link Using Selenium?
Step 1: Locating A Link in Selenium?
To click a hyperlink, you first need to find it on the webpage. Selenium offers several strategies for finding elements:
Method 1: By using the Link Text
You can find a hyperlink by its visible text:
element = driver.find_element(By.LINK_TEXT, "Link_Text")
Method 2: By use of Partial Link Text
For links with dynamic or long text, you can use a substring of the link text:
element = driver.find_element(By.PARTIAL_LINK_TEXT, "Partial Text")
Method 3: By the Use of CSS Selectors and XPath
Find the link using CSS or XPath for more accuracy:
element = driver.find_element(By.CSS_SELECTOR, "a[href='/path']")
element = driver.find_element(By.XPATH, "//a[@href='/path']")
Step 2: Clicking Links Using Selenium
Method 1: Click by Link Text
This is the simplest way to click a link using its visible text:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://example.com")
link = driver.find_element(By.LINK_TEXT, "Home")
link.click()
Method 2: Click by XPath
If the link text is not unique, use XPath to be specific:
link = driver.find_element(By.XPATH, "//a[contains(text(), 'Click Here')]")
link.click()
How to handle Dynamic Links and Redirections
How to deal with Dynamic Link Text
For links with changing or partly dynamic text use contains() in XPath:
link = driver.find_element(By.XPATH, "//a[contains(text(), 'Dynamic')]")
link.click()
How to handle Redirects After Clicking
Sometimes clicking the link creates a redirection. Have the script wait for the new page to load.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
link = driver.find_element(By.LINK_TEXT, "Next Page")
link.click()
# Wait for the new page to load
WebDriverWait(driver, 10).until(EC.url_contains("new-page"))
Conclusion
Clicking hyperlinks are the fundamental capability of Selenium, and understanding how to find and click tags is crucial for web automation. From there, you can utilize different locating strategies to make sure your automation scripts are not mimicking actions but are also resilient.
FAQs
1. How do I click a link that opens in a new tab?
Click and switch to the new tab:
link.click()
driver.switch_to.window(driver.window_handles[1])
2. Can I click a link without using find_element?
No, find_element is needed to locate the link before it can be clicked.
3. What if the link requires JavaScript to load?
Wait for the link to appear using WebDriverWait before clicking:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
link = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Link Text")))
link.click()
4. How do I verify if a link is clickable?
Use expected_conditions.element_to_be_clickable:
from selenium.webdriver.support import expected_conditions as EC
clickable = EC.element_to_be_clickable((By.LINK_TEXT, "Link Text"))
5. Can I use Selenium with hyperlinks in a headless browser?
Yes, Selenium supports headless browsers. However, make sure all the required elements are loaded correctly by adding appropriate waits.