Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Business Analyst
  3. Question
Business Analyst

selenium click link-How to click a href link using Selenium

Asked by Bernadette Bond May 4, 2021 6.2K views 3 answers
Share

About this question

 I have an html href link

using Selenium I need to click the link. Currently, I am using below code -

Driver.findElement(By.xpath("//a[text()='App Configuration']")).click(); 

But it's not redirecting to the page. I also tried below code -

 Driver.findElement(By.xpath(//a[@href ='/docs/configuration']")).click();

But this is throwing below exception -

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 13 milliseconds

The link is visible and the page is completely loaded. Can anyone help with this?

Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Feb 12, 2025

Was this helpful?

Ranjana Admin JanBask Expert

Answered on May 14, 2024

To click a link with Selenium in Python, you can use the find_element_by_link_text() or find_element_by_partial_link_text() method to locate the link by its visible text. Here's an example using find_element_by_link_text():

from selenium import webdriver
# Initialize the WebDriver (assuming Chrome in this case)
driver = webdriver.Chrome()
# Navigate to the webpage containing the link
driver.get("https://example.com")
# Find and click the link using its visible text
link_text = "Your Link Text"
link_element = driver.find_element_by_link_text(link_text)
link_element.click()
# Close the WebDriver
driver.quit()

If you prefer to locate the link by a partial text match, you can use find_element_by_partial_link_text() instead:

from selenium import webdriver
# Initialize the WebDriver (assuming Chrome in this case)
driver = webdriver.Chrome()
# Navigate to the webpage containing the link
driver.get("https://example.com")
# Find and click the link using its partial visible text
partial_link_text = "Partial Link Text"
link_element = driver.find_element_by_partial_link_text(partial_link_text)
link_element.click()
# Close the WebDriver
driver.quit()

Replace "Your Link Text" or "Partial Link Text" with the actual text of the link you want to click. Make sure to replace "https://example.com" with the URL of the webpage containing the link you want to click.


Was this helpful?

More Business Analyst discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Business Analyst Blogs

Guides, tips and career advice on Business Analyst from JanBask experts.