2

This is the HTML: https://www.dropbox.com/s/aiaw2u4j7dkmui2/Untitled%20picture.png

I don't understand why this code doesn't find the element on the page. The website doesn't use iframes.

@Test
public void Appointments() {
    driver.findElement(By.id("ctl00_Header1_liAppointmentDiary"));
}

this is the error message I get:

FAILED: Appointments
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"ctl00_Header1_liAppointmentDiary"}
4
  • The screenshot doesn't show any element with id ctl00_Header1_liAppointmentDiary, does it? There's Header1_liAppointmentDiary however... Commented Jul 25, 2013 at 9:03
  • The id on your image is Header1_liAppointmentDiary not ctl00_Header1_liAppointmentDiary Commented Jul 25, 2013 at 9:05
  • Is the element AJAX-loaded? Have you tried waiting for it, either implicitly or explicitly? Commented Jul 25, 2013 at 9:22
  • 1
    The link is currently broken. Could you fix it? Commented Feb 15, 2017 at 12:22

5 Answers 5

11

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.
@Test
public 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.*;

@Test
public void Appointments() {
    ...
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(presenceOfElementLocated(By.id("ctl00_Header1_liAppointmentDiary")))
        .doSomethingwithIt();
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

2

You're searching for ctl00_Header1_liAppointmentDiary, but there only is Header1_liAppointmentDiary, those are not the same...

ctl00_Header1_liAppointmentDiary != Header1_liAppointmentDiary

Comments

2

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"));

1 Comment

I tried that as well but I get the same error message. I've tried using findElement by xpath, but I get the same error
0

try driver.findElement(By.ClassName("MyAppointments"));

If webdriver can't find an element by xpath or id, it is usually a good idea to try all the By options that could work

http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/By.html

Comments

0

Looking at the code I think the link you are trying to click is under a drop down or you need to mouse over something to see this link. If it is so then you will have make the element visible to perform the action.

2 Comments

If it only were invisible, the element would still get found. It would not throw a NoSuchElementException. When you'd then try to interact with the element, it would throw ElementNotVisibleException.
I would expect the same, but I've just encountered this exact situation today as well. The element is not detected until I scroll down the page, making it visible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.