Selenium not finding element
Is this a timing issue? Is the element (or the whole page) AJAX-loaded? It's possible that it's not present on the page when you're trying to look for it, WebDriver is often "too fast".
To solve it, is either implicit or explicit wait.
The Implicit Wait way. Because of the implicit wait set, this will try to wait for the element to appear on the page if it is not present right away (which is the case of asynchronous requests) until it times out and throws as usual:
// Sooner, usually right after your driver instance is created.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);// Your method, unchanged.@Testpublic void Appointments() { ... driver.findElement(By.id("ctl00_Header1_liAppointmentDiary")).doSomethingWithIt(); ...}
The Explicit Wait way. This will only wait for this one element to be present on the page when looking for it. Using the ExpectedConditions
class, you can wait for different things, too - the element to be visible, clickable etc.:
import static org.openqa.selenium.support.ui.ExpectedConditions.*;@Testpublic void Appointments() { ... WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(presenceOfElementLocated(By.id("ctl00_Header1_liAppointmentDiary"))) .doSomethingwithIt(); ...}
You're searching for ctl00_Header1_liAppointmentDiary
, but there only is Header1_liAppointmentDiary
, those are not the same...
ctl00_Header1_liAppointmentDiary != Header1_liAppointmentDiary
There is no element with id="ctl00_Header1_liAppointmentDiary"
in your html
driver.findElement(By.id("ctl00_Header1_liAppointmentDiary"));
Should be
driver.findElement(By.id("Header1_liAppointmentDiary"));