how to click in href link with selenium-java

Don Gonzalo Cortez Mayer :

i'm working with selenium-java to automate some tests (it's self learning). I'm stuck in click a hyperlink, but this href is pretty particular, because like this:

<a tabindex="-1" href="../../myWebPage.html"><span>My Web Page</span></a>

My java code is:

1.- driver.findElement(By.xpath("//a[@href='../../myWebPage.html']")).click(); 2.- driver.findElement(By.xpath("//a[@href='https://RealHost/pag1/myWebPage.html']")).click();

The second option that i used is with the real link, but non of them is working.

Could you please help me?

P.S: I also used the option driver.findElement(By.LinkText("https://RealHost/pag1/myWebPage.html")).click(); but without success.

Thanks guys!

ThallsEternal :

You should be able to locate the link element by first finding its parent element by looking through the DOM and getting the xpath for that. Then use that parent element to find elements with a tag of "a"

WebElement parent = findElement(By.xpath("/*path to parent element here*/"));
parent.findElement(By.tagName("a")).click();

Note that the parent element may have multiple children of tagName "a" if that is the case use findElements() to get a collection of all the hyperlinks with that parent. Then search the collection for the one you want.

WebElement parent = findElements(By.xpath("/*path to parent element here*/"));
List<WebElement> elements = parent.findElements(By.tagName("a")).click();
//search the list for the correct link

Another thing you could try would be to locate the element by linkText.

findElement(By.linkText("/*The hyperlinks text*/")).click();

hope this helps!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=400674&siteId=1