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

 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?

Answered by Brian Kennedy
    webDriver.findElement(By.xpath("//a[@href='/docs/configuration']")).click();

The above line works fine. Please remove the space after href.

If the element is not visible please scroll down the page then perform click the action

Or

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

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

Your Answer

Answer (1)

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.


6 Months

Interviews

Parent Categories