简体   繁体   中英

Unable to click the Link in selenium webdriver

I am trying to Click on Link. But it is showing Element Not found Message. Here is my HTML Code:
<a id="expTo" class="formblue_link padRight10 exportLinkActive" style="display: block; margin-left: -50px; margin-bottom: -20px;" href="javascript:;"> Export To</a>

My code is:

        `driver.findElement(By.linkText("Export To")).click();`

It's recomanded to not try to find element by button or item text. This can be easy changed while page is still in develop so I would suggest to use click by id instead of by text

 driver.findElement(By.id("expTo")).click();

There is also alternative to click by css for example:

 driver.findElement(By.cssSelector(".css-class-name"));

All others selectors can be found here

You should try using WebDriverWait to wait until element visible and clickable as below :-

new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("expTo"))).click();

Note :- Before going to find this element, make sure it is not inside any frame/iframe . If it exists inside any frame/iframe , you need to switch that frame/iframe before finding it as :-

driver.switchTo().frame("frame/iframe name or id");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM