How to click a href link using Selenium

235,708

Solution 1

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

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

Is that element is visible in the page, if the element is not visible please scroll down the page then perform click action.

Solution 2

Use

driver.findElement(By.linkText("App Configuration")).click()

Other Approaches will be

JavascriptLibrary jsLib = new JavascriptLibrary(); 
jsLib.callEmbeddedSelenium(selenium, "triggerMouseEventAt", elementToClick,"click", "0,0");

or

((JavascriptExecutor) driver).executeScript("arguments[0].click();", elementToClick);

For detailed answer, View this post

Solution 3

Thi is your code :

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

You missed the Quotation mark

it should be as below

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

Hope this helps!

Solution 4

Use an explicit wait for the element like this:

WebDriverWait wait1 = new WebDriverWait(driver, 500);
wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("path of element"))).click();

Solution 5

Seems like the a tag is hidden. Remember Selenium is not able to interact with hidden element. Javascript is the only option in that case.

By css = By.cssSelector("a[href='/docs/configuration']");
WebElement element = driver.findElement(css);
((JavascriptExecutor)driver).executeScript("arguments[0].click();" , element);
Share:
235,708

Related videos on Youtube

Psl
Author by

Psl

Updated on August 05, 2022

Comments

  • Psl
    Psl almost 2 years

    I have a html href link

    <a href="/docs/configuration">App Configuration</a>
    

    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 page is completely loaded. I don't know what's wrong with my code.

  • Psl
    Psl almost 9 years
    getting cucumber.runtime.CucumberException: org.openqa.selenium.WebDriverException: arguments[0] is undefined
  • Saifur
    Saifur almost 9 years
    Try ((JavascriptExecutor)driver).executeScript("document.querySe‌​lector(\"a[href='/do‌​cs/configuration']\"‌​).click();"); instead of the 3 lines in the answer
  • Psl
    Psl almost 9 years
    org.openqa.selenium.WebDriverException: document.querySelector(...) is null
  • Saifur
    Saifur almost 9 years
    Seems like wait issue to me. Add some explicit wait and try
  • ikreb
    ikreb almost 7 years
    You forgot a bracket.
  • scavenger
    scavenger about 4 years
    doesn't work for pages with language based on user IP