Juhil Somaiya
Juhil Somaiya

Reputation: 953

Unable to locate element using Selenium Webdriver

I am having sticky nav bar which has few list elements where each contains href element. When I tried to locate element I am getting the error. Following is my HTML code :

 <div class="nav" id="sticky">
                <div class="container">
                    <ul class="main-nav">
                        <li><a href="Dashboard.aspx">Dashboard</a></li>
                        <li><a href="../MEFAcademicDash/StudentUI/StudentHome.aspx"
                            title="Academic Dashboard">Academic</a></li>
                        <li><a href="Notices.aspx">Notices</a></li>

I want to locate Academic through Webdriver, I am getting the error like this Unable to locate the element.

org.openqa.selenium.NoSuchElementException: Unable to locate element: /html/body/div/form/div[5]/div[1]/ul/li[2]/a

Upvotes: 1

Views: 1418

Answers (2)

Kiran Reddy
Kiran Reddy

Reputation: 130

Can you try below-mentioned xpath?

//*[text() = 'Academic']

Also, use actions driver to perform click action

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193348

As per the HTML you have shared you need to induce WebDriverWait for the element to be visible / clickable. To click() on the link with text Academic you can use the following line of code :

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='nav' and @id='sticky']/div[@class='container']/ul[@class='main-nav']//li/a[@title='Academic Dashboard' and contains(@href,'../MEFAcademicDash/StudentUI/StudentHome.aspx')]"))).click();

Upvotes: 0

Related Questions