How to handle Javascript click by using selenium Webdriver? -
am using selenium webdiver. test-case is.
- login site.
- click on notifications link.
am facing issue while clicking on notification link, having html code follows :-
<ul class="rghtsec fr menu logged"><li><a href="javascript:;"> <div class="topicon notify"><span> </span></div> <div class="mtxt">notifications<span id="rjobcntr" class="rjobcntr"></span></div></a> <div class="submenu recommendtt"> <ul> <li><a target="_blank" class="blob" id="blobid" href="http://jobsearch.naukri.com/notifications"> fetching jobs may apply for</a></li> </ul>
i have tried following 5 different ways:
/*1*/ driver.findelement(by.xpath("//a[@class='mtxt']")).click(); /*2*/ driver.findelement(by.cssselector("div[class='topicon notify']")).click(); /*3*/ driver.findelement(by.linktext("notifications")).click(); /*4*/ driver.findelement(by.xpath("//div[@class='pnotifycont dspn']")).click(); /*5*/ actions mouse=new actions(driver); webelement element=driver.findelement(by.xpath("//div[@class='pnotifycont dspn']")); mouse.movetoelement(element).click().build().perform();
error : exception in thread "main" org.openqa.selenium.nosuchelementexception: unable locate element: {"method":"xpath","selector":"//a[@class='mtxt']"} command duration or timeout: 7.56 seconds
but none of these ways resolving problem :(, can please me solve this?
you can directly click on link based on value of href
attribute:
driver.findelement(by.cssselector("a[href*='notifications']")).click();
or
driver.findelement(by.cssselector("a[href=\"http://jobsearch.naukri.com/notifications\"]")).click();