Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click a href link using Selenium

Tags:

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.

like image 890
Psl Avatar asked Jun 04 '15 04:06

Psl


People also ask

How do you click a link with hyperlink in Selenium?

To click a link, we can use the link text locator which matches the text enclosed within the anchor tag. We can also use the partial link text locator which matches the text enclosed within the anchor tag partially. NoSuchElementException is thrown if there is no matching element found by both these locators.

Can we use HREF in Selenium?

We can get an attribute value from a href link in Selenium. To begin with, we have to first identify the element having an anchor tag with the help of any of the locators like css, id, class, and so on. Next, we shall use the getAttribute method and pass href as a parameter to the method.

What is the command to click on a hyperlink in Selenium Webdriver?

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


1 Answers

 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.

like image 71
Saritha G Avatar answered Oct 07 '22 16:10

Saritha G