How to click a href link using Selenium
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);
Related videos on Youtube
Psl
Updated on August 05, 2022Comments
-
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 almost 9 yearsgetting cucumber.runtime.CucumberException: org.openqa.selenium.WebDriverException: arguments[0] is undefined
-
Saifur almost 9 yearsTry
((JavascriptExecutor)driver).executeScript("document.querySelector(\"a[href='/docs/configuration']\").click();");
instead of the 3 lines in the answer -
Psl almost 9 yearsorg.openqa.selenium.WebDriverException: document.querySelector(...) is null
-
Saifur almost 9 yearsSeems like wait issue to me. Add some explicit wait and try
-
ikreb almost 7 yearsYou forgot a bracket.
-
scavenger about 4 yearsdoesn't work for pages with language based on user IP