I have written driver.findElement(By.id("kfiDocumentLink")).click(); code for clicking on the button 'KFI Document'.
Please find the HTML code.
<a class="button" id="kfiDocumentLink" href="/Quote/KFIDocument/The%20Co-operative%20Bank%20-%20Download%20Mortgage%20Illustration%20(PDF)%20160808104103" target="_blank">Download Mortgage Illustration (PDF)</a>
When I run the code, sometimes I am able to click on the button and sometimes I am unable to click the button.
Could someone assist on this please?
Actually some time when you goes to find element, It would not be present on the DOM at that time due to slow internet or other reason, that's why are sometime able to click and sometime not.
To overcome this issue you should try using WebDriverWait with ExpectedConditions.elementToBeClickable to wait before click on element until element visible on the DOM and clickable as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
el = wait.until(ExpectedConditions.elementToBeClickable(By.id("kfiDocumentLink")));
el.click();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With