28

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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Psl
  • 3,830
  • 16
  • 46
  • 84

12 Answers12

34
 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.

Saritha G
  • 2,588
  • 1
  • 15
  • 27
11

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

Community
  • 1
  • 1
Arjit
  • 3,290
  • 1
  • 17
  • 18
4

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!

Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53
evan
  • 41
  • 1
3

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();
Generic Bot
  • 309
  • 1
  • 4
  • 8
Juhil Somaiya
  • 873
  • 7
  • 20
2

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);
Saifur
  • 16,081
  • 6
  • 49
  • 73
  • getting cucumber.runtime.CucumberException: org.openqa.selenium.WebDriverException: arguments[0] is undefined – Psl Jun 04 '15 at 05:16
  • Try `((JavascriptExecutor)driver).executeScript("document.querySelector(\"a[href='/docs/configuration']\").click();");` instead of the 3 lines in the answer – Saifur Jun 04 '15 at 05:20
  • org.openqa.selenium.WebDriverException: document.querySelector(...) is null – Psl Jun 04 '15 at 05:28
  • Seems like wait issue to me. Add some explicit wait and try – Saifur Jun 04 '15 at 05:32
1

How to click on link without using click method in selenum?

This is a tricky question. Follow the below steps:

driver.get("https://www.google.com");
String gmaillink= driver.findElement(By.xpath("//a[@href='https://mail.google.com/mail/?tab=wm&ogbl']")).getAttribute("href");
System.out.println(gmaillink);
driver.get(gmaillink);
double-beep
  • 5,031
  • 17
  • 33
  • 41
1

Try to use Action class to reach the element

Actions action = new Actions(driver);
action.MoveToElement(driver.findElement(By.xpath("//a[text()='AppConfiguration']")));
action.Perform();
1

These answers seem a bit too complicated. This worked for me. Give the svg tag an id and then find the Element by that id:

This is your svg:

<svg id="element-id"><use href="/...."></use></svg>

Then do it like this:

driver.findElement(By.id("element-id")).click();
Valley
  • 11
  • 1
0

You can use this method:

For the links if you use linkText(); it is more effective than the any other locator.

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

To click() on the element with text as App Configuration you can use either of the following Locator Strategies:

  • linkText:

    driver.findElement(By.linkText("App Configuration")).click();
    
  • cssSelector:

    driver.findElement(By.cssSelector("a[href='/docs/configuration']")).click();
    
  • xpath:

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

Ideally, to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • linkText:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("App Configuration"))).click();
    
  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href='/docs/configuration']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='/docs/configuration' and text()='App Configuration']"))).click();
    

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Why not try using this one : .name of the class > a

What we mean is fetch the class followed by its href

e.g. could be (.offset3 >a).click() , here offset3 is the class and a is the anchor tag

sample code enter image description here

Rohit Bahadur
  • 61
  • 1
  • 3
-1

You can use xpath as follows, try this one :

driver.findElement(By.xpath("(.//[@href='/docs/configuration'])")).click();
Rahul
  • 10,830
  • 4
  • 53
  • 88