Difficulty clicking an anchor tag using href as locator

274 views Asked by At

How to locate identifiers in Selenium for the following line of code?

<a href="loginPage" class="loginBtn" data-toggle="modal" data-target="rnr-loginPanel">Login</a>

I have tried:

driver.findElement(By.xpath("//a [contains ( @href = 'loginPage' )]")).click(); 

but it is not working for me.

1

There are 1 answers

0
Vikas Ojha On BEST ANSWER

There is a syntax problem with the contains keyword in your xpath. Following should work -

driver.findElement(By.xpath("//a[contains(@href,'loginPage')]")).click();

Also, a better and easier way would be

driver.findElement(By.className("loginBtn")).click();